How to find out which .jar file a certain Java class belongs to

jar display classes

One of the problems that we always face software developers It is that of disorder, and it is very simple to start having files scattered throughout the system once we begin to test different versions of our programs (for example when we modify some functionality within it). And the same happens with source code files, which can host different classes and then it is difficult for us to find which one each can belong to.

In the case of Java, the most common is to need find which JAR file a certain Java class belongs to in order to solve the typical problems of 'NoClassDefFoundError' something that luckily is not too complicated as we will see below. But before that we will explain that JARs are basically compressed files, in such a way that in order to examine them, an alternative is to decompress them or look for an alternative that allows us to 'look' inside them.

For this we can execute the following, assuming that we are going to review a file called resistor (it is from a very simple app that I have created to calculate the value of a resistance), but of course in each case it is replaced with the name of the file that we want review:

$ jar tvf resistor.jar

The result will be the one we visualize in the upper image that accompanies this post, and where we have visible all the files that make up a certain JAR, among which of course are the famous .class that make up the classes of an application, with which we will finally know which are the files that contain the classes we are looking for.

Now, even though in the case of Linux we always have before us a way of doing things from the terminal, for many users it is more practical to do them from a graphical application, even if it implies installing additional software on their system. For them we have a program called jar-explorer, which allows us to explore JAR files to view all their content, and the good thing is that we have an integrated search tool, which offers recursive searches of Java classes.

It is an application developed, of course, in Java, so to install it in Ubuntu we execute:

$wget http://jar-explorer.googlecode.com/files/jarexplorer-0.7-BETA.jar
$ java -jar jarexplorer-0.7-BETA.jar

Now, once the application starts, what we have to do is go to the option of File -> Find root directory or Jar file, and select the directory in which we have the JAR files. We are going to see a list of all the files, in the section 'Jar File List', and then all we have to do is enter the name of the class we are looking for, which we do in the text field located to the right of 'Enter class to search'at the top of the screen. Finally, click on the button 'Start' to begin the search, which will take no more than seconds.

However jar-explorer It still has one more characteristic that can be very useful to us and that is allow us to explore the contents of each class definition in Java, something that we can achieve by just clicking on the class name (from the previous search results).

That's all, as we can see, these are two different but totally valid approaches to search for Java classes among our JAR files, a task in which developers fall more than once and that does not have to be tedious or complicated.


Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.