Over a million developers have joined DZone.

Remove Unnecessary Jars!

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

Your application may turn into Jar garbage maybe not in small projects but in projects which has an increasing diameter more and more. I’m sure once upon a time you thought to have an application and then remove the unnecessary Jars with it. We use Jar libraries in our projects sometimes for a need and sometimes for developing software easily. One of the biggest reasons of why projects turn into Jar garbage may result from not knowing which library serves for what. Thus, due to the dependencies we include Jar libraries in projects, explicitly whatever we find, because of not getting compile and runtime exceptions. For this purpose, I want to introduce you an excellent software tool to detect unused Jars.

LooseJar LooseJar is software that discovers which Jar file is used how often through Java Instrumentation library. Instrumentation is a standard library which serves measurement process about code without affecting the ordinary operation of Java codes and allows obtaining a variety of information.

How to Run LooseJar? You have to define software as Java agent in your application’s JVM parameter. For example:

java -javaagent:<LooseJar Path> <Your ClassName> -cp <Your Jars>
You can use Java agent as in the above syntax example. If your application is a web application, you can define Java agent at JVM settings of the default application server. Point to be considered here is testing points dependent to all libraries from running to termination of the application in order to obtain detection about which library is used how often. Unit tests and BDD tests can be used to measure the overall Looser application and Selenium tests can be used if web application is being used. For example you have a method named getSQLConnection(). This method requires sql connection and uses mysql connector driver in the background. If this method is not called when Looser agent is active, Looser agent provide you information about mysql dependency is not being used. This point must be considered in the use of Looser.

Conclusion So, how Looser will give us information about which Jar is used how much? There are two ways to obtain this information. Firstly, this information is provided as a console output immediately after the application is terminated. The second way is accessing with JMX connection in the run-time and accessing to summary() method of Looser as MBean method. JMX Jconsole or VisualVM can be used to access with JMX.

A sample output:

If your applications' result contains any 0.00% rated Jars, you can remove it from CLASSPATH. 

Real content above, can be accessed at http://en.kodcu.com/2013/06/remove-unnecessary-jars/

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}