How to find which jar file a Java class belongs to

Last updated on June 27, 2020 by Dan Nanni

Suppose you have multiple jar files, and want to know which jar file contains a given Java class definition. This kind of situation can arise when you are dealing with NoClassDefFoundError in your Java program.

The JAR (Java ARchive) format is built on ZIP file format, so in order to check what Java class definitions are available in a given jar file, you need to check the jar file in an uncompress form. Is there any convenient way to inspect all jar files in search for Java class definition?

Method One

In fact, it is not that difficult to find out a list of Java classes contained in a given jar file. With tvf option, jar command can show you a table of contents in jar file. For example:

$ jar tvf gson-2.2.2.jar
     0 Mon Jul 02 18:04:46 EDT 2012 META-INF/
   705 Mon Jul 02 18:04:44 EDT 2012 META-INF/MANIFEST.MF
     0 Mon Jul 02 18:04:40 EDT 2012 com/
     0 Mon Jul 02 18:04:40 EDT 2012 com/google/
     0 Mon Jul 02 18:04:40 EDT 2012 com/google/gson/
     0 Mon Jul 02 18:04:40 EDT 2012 com/google/gson/stream/
     0 Mon Jul 02 18:04:40 EDT 2012 com/google/gson/internal/
     0 Mon Jul 02 18:04:40 EDT 2012 com/google/gson/internal/bind/
     0 Mon Jul 02 18:04:40 EDT 2012 com/google/gson/annotations/
     0 Mon Jul 02 18:04:40 EDT 2012 com/google/gson/reflect/
   732 Mon Jul 02 18:04:40 EDT 2012 com/google/gson/JsonParseException.class
  1001 Mon Jul 02 18:04:40 EDT 2012 com/google/gson/Gson$1.class
   413 Mon Jul 02 18:04:40 EDT 2012 com/google/gson/JsonDeserializationContext.class
   207 Mon Jul 02 18:04:40 EDT 2012 com/google/gson/FieldNamingStrategy.class
  4762 Mon Jul 02 18:04:40 EDT 2012 com/google/gson/DefaultDateTypeAdapter.class
. . . .

Leveraging this feature of jar command, you can run the following to find the jar file that contains a specific Java class definition. In this example, I want to know from what jar file com.google.gson.JsonParser.class is loaded. Run this command in the directory where jar files are located.

$ find . -name "*.jar" | while read JAR; do jar tvf $JAR | grep JsonParser && echo $JAR ; done
  2594 Mon Jul 02 18:04:40 EDT 2012 com/google/gson/JsonParser.class ./gson-2.2.2.jar

Method Two

Another approach is to use a GUI program called jar-explorer, which allows you to explore the content of individual jar files, as well as recursively search for Java classes in all jar files in a directory. To install and run jar-explorer, do the following.

$ wget https://github.com/javalite/jar-explorer/archive/v0.7.tar.gz
$ tar -xf v0.7.tar.gz
$ cd jar-explorer-0.7/bin
$ java -jar jarexplorer-0.7.jar

Once you have launched jar-explorer, go to File → "Locate Root Directory or Jar File" menu. Select the directory where jar files (and any subdirectories containing jar files) are located. Under "Jar File List" upper window, you will then see a list of all existing jar files. Enter the name of Java class you are searching for (e.g., JsonParser), in the search blank at the top.

As you can see in the screenshot above, there is one jar file (e.g., gson-2.2.2.jar) that contains JsonParser.

Using jar-explorer, you can also explore the content of each Java class definition, simply by clicking on the class name.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean