Dr. Hans-Peter Störr
Dr. Hans-Peter Störr

Reputation: 25976

Find out which Java classes are actually loaded and reduce jar

Is there a way to automatically find out which Java classes are actually loaded (either during compile time, as far as that's possible, or during the runtime of an application), and to throw out all other classes from a JAR to create a smaller JAR? Does that actually make sense in practice?

I am talking about the application classes for an application JAR. Usually there are lots of libraries in an application, and an application rarely needs all features of those libraries. So I suspect that would make a considerably smaller application. In theory that might be done for example via an Java agent that logs which classes and resources are read by one or several runs of an application (or even just by java -verbose:class), and a maven plugin that throws out all other classes from a jar-with-dependencies. Is there already something like that?

Clarification: I am not talking about unused dependencies (JARs that are not used at all), but about removing unused parts of each included JAR.

Upvotes: 7

Views: 697

Answers (3)

Yan Khonski
Yan Khonski

Reputation: 13083

Global strategy.

1) Find all the classes that are loaded during runtime.

2) List of all the classes available in the classpath.

3) Reduce your class path by creating copies of jars containing only classes you need.

I have done 1 and 2 part so I can help you.


1) Find out all the classes that are loaded. You need 100 % code coverage (I am not talking about tests, but production). So run all possible scenarios, so all the classes your app needs will be loaded and logged.

To log loaded classes try several approaches. Reflection, –verbose:class flag, also you can learn about java agent. It allows to modify methods during runtime. This is an example of some java agent code or another java agent example

2) To find all the classes available in jar, you can write a program. You need to know all places where application jars are placed. Loop throw these jars (You can use ZipFile), loop through ZipFileEntry entries, and collect all classes.

3) After that write a script or program that reassembles your application. For example, now you can create a new jar file for each library and put there only needed classes.

Also you may use a tool (again, you are a programmer, so write a program), which checks code for classes dependence. You do not want to remove classes if they are used for compilation. When I was a student, I wrote code alanyzer, which builds an oriented graph for classes dependencies.

As @Gokul Nath KP notes, I did this before. I manually change gradle and maven dependencies, removing one by one, and then full regression test. It took me a week (our application was small comparing to modern world enterprise systems created by hundreds of developers).

So, be creative, and in case of success, your project will be used by millions!

Upvotes: 0

Florian Albrecht
Florian Albrecht

Reputation: 2326

Well, the Maven Shade Plugin has an option minimizeJar when creating an Uber-JAR for your application:

https://maven.apache.org/plugins/maven-shade-plugin/

But, as others already pointed out, this is quite dangerous, as it regularly fails to detect class accesses which are done via Reflection or other dynamic references.

Upvotes: 1

Gokul Nath KP
Gokul Nath KP

Reputation: 15553

It may not be a good approach automate, as application can use reflection to initialise objects or one JAR is dependent on another JAR.

Only way that I can think of is to remove each JARs one by one and check if application runs as expected. Then again in this approach all modules of the application has to be tested, since one module can work without particular dependency and other may not.

Better solution is to take care while developing. The application developer must be careful in adding a dependency and removing unwanted dependency after his/her piece of code is done.

Upvotes: 0

Related Questions