ClassPathUtilities { public static File findFileOnClassPath(final String fileName) { final String classpath = System.getProperty("java.class.path"); final String pathSeparator = System.getProperty("path.separator"); final StringTokenizer tokenizer = new StringTokenizer(classpath, pathSeparator); while (tokenizer.hasMoreTokens()) { final String pathElement = tokenizer.nextToken(); final File directoryOrJar = new File(pathElement); final File absoluteDirectoryOrJar = directoryOrJar.getAbsoluteFile(); if (absoluteDirectoryOrJar.isFile()) { final File target = new File(absoluteDirectoryOrJar.getParent(), fileName); if (target.exists()) { return target; } } else { final File target = new File(directoryOrJar, fileName); if (target.exists()) { return target; } } } return null; } private  ClassPathUtilities(); static File findFileOnClassPath(final String fileName);  }
@Test public void findFileNotInClasspath() { File resultFile = ClassPathUtilities.findFileOnClassPath("bla"); assertNull(resultFile); }
@Test public void findFileOnClasspath() throws Exception { File testFile = fileUtils.createFileInTempDir("testFile"); addFileToClasspath(testFile); File resultFile = ClassPathUtilities.findFileOnClassPath("testFile"); assertNotNull(resultFile); assertEquals(testFile.getAbsolutePath(), resultFile.getAbsolutePath()); }
@Test public void findDirectoryOnClasspath() throws Exception { File testDir = fileUtils.createDirInTempDir("testDir"); File testFile = fileUtils.createFileInDir(testDir, "testFile"); addFileToClasspath(testDir); File resultFile = ClassPathUtilities.findFileOnClassPath("testFile"); assertNotNull(resultFile); assertEquals(testFile.getAbsolutePath(), resultFile.getAbsolutePath()); }