TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

 Home » Tip Bank » .NET » VB.NET » Classes
Language: Java
Feb 8, 1999

# A Basic Class Finder Utility

Here's a useful utility for finding the full pathname for a class (or any other file) from a Java program. This utility uses the method getClassNameFromPackage() described in the Tip "Obtaining a File Path From a Class Name." This utility lets you find the complete file path for any class that can be found in the system classpath.

public class ClassFinder {
private static final String classPath=System.getProperty("java.class.path");
private static final String pathSeparator=System.getProperty("path.separator");

public static final String getFullPath(String fileString) {
File file1 = new File(fileString);
String dirname = file1.getParent();
if (dirname == null)
dirname = "";
String filename = file1.getName();

StringTokenizer st = new StringTokenizer(classPath, pathSeparator);
String token = null;
while (st.hasMoreTokens()) {
token = st.nextToken();

if (token.endsWith(dirname)) {
String absolutePath = token + fileSeparator + filename;
if ((new File(absolutePath)).exists())
return absolutePath;
}
}
return null;
}

public static void main (String[] args)
{
// getPathFromClassName() in Tip "Obtaining a file path from a class name"
String filepath = ClassFinder.getPathFromClassName(args[0]);
String absolutePath = getFullPath(filepath);
System.out.println("Absolute path = " + absolutePath);
}
}

Lines 2-3 obtain the JVM's current class path and the system file separator (i.e. the delimiter for separating paths in the system's file path, like ":" and ";"). The method getFullPath() takes in a String (fileString) and returns the absolute path for the file based on the classPath variable. The fileString may be just the name of the file (i.e. MyClass.class) or the relative path name (i.e. foo\bar\baz\MyClass.class).

Lines 6-10 split the fileString into the name of the file and its directory. See Tips "Manipulating File Names With the File Class" and "Performing File Operations With the File Class" for a discussion of how to use the File methods exists(), getParent(), and getName(). If we had started with a fileString foo\bar\baz\MyClass.class, the variable dirname would contain foo\bar\baz and the variable filename would contain MyClass.class.

Line 12 declares a StringTokenizer to split the pathString into individual directory paths. The loop in Lines 13-24 checks each path to see if the class file exists. Line 17 checks if the directory path ends in dirname (i.e., is the package in the directory path). On Line 18, the absolute file path is constructed. If the file exists, the If it does, the absolute path is returned on Line 20. If the file is not found, the method returns a null on Line 23.

Lines 26-32 comprise the main routine to use this utility. On Line 29, the path for the class name (passed in as an argument to the program) is obtained. On Line 30, this is passed to the getFullPath() method to obtain the absolute path. On Line 31, the result is printed out. For example, if the system's classpath contained a path C:\This\That and the class foo.bar.baz.MyClass existed under that path, then running ClassFinder would have the following result:


$java ClassFinder foo.bar.baz.MyClass$ Absolute path = C:\This\That\foo\bar\baz\MyClass.class

Note that passing bar.baz.MyClass, baz.MyClass, or just MyClass would still get you the same result.
Ajit Sagar

 Submit a Tip Browse ".NET" Tips Browse All Tips
Comment and Contribute

(Maximum characters: 1200). You have 1200 characters left.

Thanks for your registration, follow us on our social networks to keep up-to-date