Mar 9, 2005 #1 kanghao IS-IT--Management Jul 4, 2004 68 KR How can I determine which jar file I use for a certain class. For example, org.w3c.dom.Element is available from so many jar files xalan, apache, jdom etc. I want to know which *.jar file is in used. thanks.
How can I determine which jar file I use for a certain class. For example, org.w3c.dom.Element is available from so many jar files xalan, apache, jdom etc. I want to know which *.jar file is in used. thanks.
Mar 9, 2005 #2 stefanwagner Programmer Oct 19, 2003 2,373 DE perhaps Code: protected URL ClassLoader.findResource (String name) ? (didn't try it myself) seeking a job as java-programmer in Berlin: http://home.arcor.de/hirnstrom/bewerbung Upvote 0 Downvote
perhaps Code: protected URL ClassLoader.findResource (String name) ? (didn't try it myself) seeking a job as java-programmer in Berlin: http://home.arcor.de/hirnstrom/bewerbung
Mar 24, 2005 Thread starter #3 kanghao IS-IT--Management Jul 4, 2004 68 KR Almost! JWhich works. public class JWhich { public static void main(String[] args) { if(args.length > 0) { JWhich.whichByClass(args[0]); } else { System.err.println("Usage: java JWhich <classname>"); } } public static void whichByClass(String className) { if(!className.startsWith("/")) { className = "/" + className; } className = className.replace('.', '/'); className = className + ".class"; java.net.URL classUrl = new JWhich().getClass().getResource(className); if(classUrl != null) { System.out.println("\nClass '" + className + "' found in \n'" + classUrl.getFile() + "'"); } else { System.out.println("\nClass '" + className + "' not found in \n'" + System.getProperty("java.class.path") + "'"); } } public static String whichByObject(Object objName){ Class c = objName.getClass(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); String cn = c.getName().replaceAll("\\.","/") + ".class"; String path = cl.getResource(cn).getFile(); return path; } } Upvote 0 Downvote
Almost! JWhich works. public class JWhich { public static void main(String[] args) { if(args.length > 0) { JWhich.whichByClass(args[0]); } else { System.err.println("Usage: java JWhich <classname>"); } } public static void whichByClass(String className) { if(!className.startsWith("/")) { className = "/" + className; } className = className.replace('.', '/'); className = className + ".class"; java.net.URL classUrl = new JWhich().getClass().getResource(className); if(classUrl != null) { System.out.println("\nClass '" + className + "' found in \n'" + classUrl.getFile() + "'"); } else { System.out.println("\nClass '" + className + "' not found in \n'" + System.getProperty("java.class.path") + "'"); } } public static String whichByObject(Object objName){ Class c = objName.getClass(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); String cn = c.getName().replaceAll("\\.","/") + ".class"; String path = cl.getResource(cn).getFile(); return path; } }