Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find unnecessary jars in a project

Tags:

java

maven

jar

I have some jars in my project. I want to know how these jars are used or what the effects will be if I remove them?

like image 309
user3153014 Avatar asked Jan 02 '14 09:01

user3153014


People also ask

How do you remove unused JARs?

Remove -> Test -> Remove -> Test -> Remove -> Hope you enjoy :) Change project to maven and select only jar you wanna use. For me this is the easiest way.

What are JARs in eclipse?

A JAR (Java Archive) file is a ZIP format file that bundles Java classes into a single unit. There are two types of JAR file in Java: Library JAR (normal JAR) files: JARs which are reusable libraries.

What is jar file in Java with example?

JAR stands for Java ARchive. It's a file format based on the popular ZIP file format and is used for aggregating many files into one. Although JAR can be used as a general archiving tool, the primary motivation for its development was so that Java applets and their requisite components (.


2 Answers

You can achieve it using loosejar.

It is a simple Java Agent that can be used to discover unnecessary jars lying on application classpath. It performs per classloader JVM heap analysis and displays its results. loosejar can be safely used during development, QA, UAT or even in production as it doesn't modify the state of the JVM at all and adds no overhead.

How to use:

  1. Start your application or application server with -javaagent:loosejar.jar flag (loosejar.jar should obviously point to the correct path of the actual jar)

  2. Exercise your application to make sure that the classes get loaded into the JVM.

  3. Get loosejar analysis results via JMX console (open jconsole and run com.googlecode.loosejar.LooseJarMBean#summary() in MBeans folder) or on application shutdown (via regular console log).

like image 78
Ankur Lathi Avatar answered Oct 15 '22 17:10

Ankur Lathi


You are using Maven, so you can use Maven's features to have a look what jars you need. Run the following in the root of your project:

mvn dependency:analyze

And you'll see the line [WARNING] Unused declared dependencies found: with a list of dependencies that you don't actually need.

like image 42
Jakub Kotowski Avatar answered Oct 15 '22 17:10

Jakub Kotowski