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.

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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a