public class ClassFinder { public static void main(String[] args) throws Exception { String className = System.getProperty("className"); System.out.println("Going to search " + className); ClassLoader loader = Thread.currentThread().getContextClassLoader(); System.out.println("Context ClassLoader is " + loader); String path = className.replace('.', '/').concat(".class"); Enumeration<URL> enu = ((URLClassLoader)loader).findResources(path); while (enu.hasMoreElements()) { System.out.println(enu.nextElement()); } } }
ant から使うとクラスパスが柔軟に設定できていいと思う。
... <path id="build.classpath"> <fileset dir="lib"> <include name="*.jar"/> </fileset> <fileset dir="${jboss.home}/client"> <include name="*.jar"/> </fileset> <fileset dir="${jboss.home}/server/${server.name}/lib"> <include name="**/*.jar"/> </fileset> </path> <target name="run" depends="compile"> <java classname="test.ClassFinder" fork="true"> <sysproperty key="className" value="${className}"/> <classpath refid="build.classpath"/> </java> </target> ...
[tkobayas@tkobayas JarSearch]$ ant run -DclassName=javax.ejb.Stateless Buildfile: build.xml compile: run: [java] Going to search javax.ejb.Stateless [java] Context ClassLoader is sun.misc.Launcher$AppClassLoader@17182c1 [java] jar:file:/home/tkobayas/usr/local/jboss-eap-4.3.0.GA_CP07/jboss-as/client/jboss-ejb3x.jar!/javax/ejb/Stateless.class [java] jar:file:/home/tkobayas/usr/local/jboss-eap-4.3.0.GA_CP07/jboss-as/server/default/lib/jboss-ejb3x.jar!/javax/ejb/Stateless.class BUILD SUCCESSFUL Total time: 2 seconds