This example program demonstrates how to find a file in the classpath. The simple approach is to get the classpath string using the System.getProperty() method which returns the complete string value of the classpath. Then you can split the values using the stringtokenizer and search eacy token against the file name.
Lets look at the example:
package javabeat.net.core; import java.io.File; import java.util.StringTokenizer; /** * This example program detects the file name in the * classpath by taking the file name as the input. * * @author krishna * */ public class JavaCoreExample { public static void main(String[] args) { File classpathFile = search("<<File Name>>"); System.out.print(classpathFile.getName()); } /** * Searching the file name * @param fileName * @return */ public static File search(final String fileName) { final String classpathStr = System.getProperty("java.class.path"); final String separator = System.getProperty("path.separator"); final StringTokenizer strTokenizer = new StringTokenizer(classpathStr, separator); while (strTokenizer.hasMoreTokens()) { final String pathElement = strTokenizer.nextToken(); final File directory = new File(pathElement); final File absoluteDirectory = directory .getAbsoluteFile(); if (absoluteDirectory.isFile()) { final File target = new File( absoluteDirectory.getParent(), fileName); if (target.exists()) { return target; } } else { final File target = new File(directory, fileName); if (target.exists()) { return target; } } } return null; } }