Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Get Path Where A Class Is Located

10 views
Skip to first unread message

gimme_this...@yahoo.com

unread,
Dec 7, 2004, 5:02:04 PM12/7/04
to
Try this ...

Create the following ksh utility and put it in your local bin
directory.
Call it jwhich.

#!/bin/ksh

/usr/j2se/bin/java -classpath $CLASSPATH:/home/emmmcl01/bin JWhich $*

And then use the class file compiled from the code below:

This class does sort of what you want, it locates the class in the jar
files on disk in your classpath.

This gives you a utility that works like

jwhich org.jdom.Element
/home/me/jars/jdom.jar


public class JWhich
{
/**
* Returns the absolute pathname of the class file
* containing the specified class name, as prescribed
* by the current classpath.
*
* @param className The full (package-qualified) name of the class.
* @return the absolute pathname of the file containing the class
specified by <code>className</code>
*/
public static String which( String className )
{
if ( !className.startsWith("/") )
{
className = className.replace( '.', '/' );
className = className + ".class";
className = "/" + className;
}

java.net.URL classUrl = new JWhich().getClass().getResource(
className );

if ( null == classUrl ) return( null );
return( classUrl.getFile() );

}//which( String )


public static void main( String args[] )
{
if ( args.length > 0 )
{
String className = args[0];
String classfilePath = JWhich.which( className );

if ( null != classfilePath )
{
System.out.println( classfilePath );
}
else
{
System.out.print( "Class '" + className + "'" );

char cpSep = System.getProperty( "path.separator" ).charAt(0);
String classPath = System.getProperty( "java.class.path" );
classPath = classPath.replace( cpSep, '\n' );

System.out.println( " not found in\n" + classPath );
}
}
else
{
System.err.println( "Usage: java " + JWhich.class.getName() + "
<classname>" );
}

}//main( String args[] )

}//class JWhich

0 new messages