How to find which jar file a Java class belongs to

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

Actually it is not that difficult. 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 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
$ java -jar jarexplorer-0.7-BETA.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.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

Leave a comment

Your email address will not be published. Required fields are marked *