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.
Jayan
source share