Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

determining jar file in use.

Status
Not open for further replies.

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.
 
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;
}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top