devxlogo

A Basic Class Finder Utility

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. fooarazMyClass.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 fooarazMyClass.class, the variable dirname would contain fooaraz and the variable filename would contain MyClass.class.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email

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:ThisThat 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:ThisThatfooarazMyClass.class

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

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist