Remove unused banks from the project


The easiest way (tool?) To remove unused banks from my java (ant) project. Our project has become really huge, and we want to do the cleaning. There are several jars that are added to the classpath, but not all of them are used to compile / run. Is there a way to identify unnecessary banks by running some utility from the command line?
Note : we do not want to follow the tedious process of removing one or more cans, and then compile / run to check whether they are needed or not.

+7
java jar ant
source share
4 answers

This is what I finally did. I used the JBOSS TattleTale ( http://www.jboss.org/tattletale ) to identify unused jars at compile time (he reported that 17 jars are not used).
Then, for each of the cans in this list, I did a string search in my project to find out if the highest level of the package in the jar was used (this is an attempt to see if any class belonging to this bank was loaded using reflection).
Thus, I was able to successfully eliminate 6 cans from my project.
Since the number of unused jars at compile time was small, this approach was quick.

+9
source share

If this is the best solution, I would like to hear it.

The problem is that even if a bank is not required for compilation, it could be started. And this β€œneed” can be transitive. EG: A used jar needs a jar that you don't use, and only at runtime. Like the nature of runtime errors, they will only be displayed when trying to execute code. Worst case that may not happen until you start production.

The best solution that I know of is exactly what you don’t want to do: "remove one or more cans, then compile / run to check if they are needed or not."

+4
source share

Delete β†’ Test β†’ Delete β†’ Test β†’ Delete β†’ I hope you like it :)

Change the project to maven and select only the jar that you want to use. This is the easiest way for me.

+2
source share

As tieTYT noted in his answer, many banks are only needed at compile time. (e.g. jdbc jars implementation)

Another approach could be to create a java agent and collect all classes. Then you can map these class names to jar and see if any jars are needed.

Agent sample

package com; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; import java.lang.instrument.Instrumentation; import java.security.ProtectionDomain; public class NameAgent { public static void premain(String agentArgs, Instrumentation inst) { inst.addTransformer(new Tranformer(), true); } static class Tranformer implements ClassFileTransformer { @Override public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { System.out.println("loading-class:" + className); return classfileBuffer; } } } 

Build.xml ... for ant

  <project name="namedump" default="dist" basedir="."> <property name="src" value="src"/> <property name="build" value="build"/> <property name="dist" value="dist"/> <target name="init"> <mkdir dir="${build}"/> <mkdir dir="${dist}"/> </target> <target name="compile" depends="init"> <javac srcdir="${src}" destdir="${build}" optimize="true" includeantruntime="false"/> </target> <target name="dist" depends="compile"> <jar jarfile="${dist}/namecollector.jar"> <fileset dir="${build}"/> <manifest > <attribute name="Premain-Class" value="com.NameAgent" /> <attribute name="Can-Redefine-Classes" value="true" /> <attribute name="Can-Retransform-Classes" value="true" /> </manifest> </jar> <pathconvert property="absdist" dirsep="/"> <path location="${dist}"/> </pathconvert> <echo> To use, add the following to the JVM command-line options. -javaagent:${absdist}/namecollector.jar </echo> </target> <target name="clean"> <delete dir="${build}"/> <delete dir="${dist}"/> </target> </project> 

Another approach that we tried to use on Windows was that banks are blocked when classes are loaded. Launch the application and try removing from the runtime. If it was used, the deletion should fail. Not sure how safe this is. I know that it does not work on Linux, etc. Files can be deleted without problems.

+1
source share

All Articles