Re: Get Path Where A Class Is Located

gimme_this_gimme_that_at_yahoo.com
Date: 12/07/04


Date: 7 Dec 2004 14:02:04 -0800

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



Relevant Pages

  • Re: Create Forms via Parameter
    ... need the classname to retrieve a class registered via RegisterClasses. ... Function ShowFormByName(const formname: String; ... variable, the form reference, and that can be had the way shown above. ... The VCL contains a suitable class registry, ...
    (borland.public.delphi.language.objectpascal)
  • Re: DirectCom.dll: vb sample -> vfp
    ... GETINSTANCE erwartet als String-Parameter jeweils den Pointer ... Dim D As Object, FName As String, ClassName As String ... ClassName = StrConv'nach ANSI ...
    (microsoft.public.de.fox)
  • Re: String problem VS 2005
    ... the console the Classname and the text in the notepad window. ... The first output to the console is "N" and not ... I dont understand how to work with all the C++ string format. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Falsch Typ Erkennung ?
    ... Ich habe auch schon mit NHibernate gearbeitet. ... linke Seite immer ein "object" Typ deklariert. ... foreach(string className in classNames) ... object list = NHibernateMethod(className); ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: The curse of constant fields
    ... I think everything you need is in the class file. ... I think they make certain guarantees about the initialisation of constant expressions - the order, or not needing to load other classes to do them, or something. ... The definition of constant expressions is something like expressions of string or primitive type that are literals, references to other constant expressions, and compound expressions built from other constant expressions. ... public class Foo { ...
    (comp.lang.java.programmer)