Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Remove Unnecessary Jars!

DZone's Guide to

Remove Unnecessary Jars!

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

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/

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}