/** * Retrieve the location that this classfile was loaded from, or possibly null if the class was compiled on the fly */ public static File get(Class<?> clazz) { // Get the location of this class ProtectionDomain pDomain = clazz.getProtectionDomain(); CodeSource cSource = pDomain.getCodeSource(); // file:/X:/workspace/XYZ/classes/ when it's in ide/flat // jar:/X:/workspace/XYZ/jarname.jar when it's jar URL loc = cSource.getLocation(); // we don't always have a protection domain (for example, when we compile classes on the fly, from memory) if (loc == null) { return null; } // Can have %20 as spaces (in winxp at least). need to convert to proper path from URL try { File file = new File(normalizePath(loc.getFile())).getAbsoluteFile() .getCanonicalFile(); return file; } catch (UnsupportedEncodingException e) { throw new RuntimeException("Unable to decode file path!", e); } catch (IOException e) { throw new RuntimeException("Unable to get canonical file path!", e); } }
URL getResource(String resourceName) { try { resourceName = normalizePath(resourceName); } catch (IOException e) { e.printStackTrace();
InputStream getResourceAsStream(String resourceName) { try { resourceName = normalizePath(resourceName); } catch (IOException e) { e.printStackTrace();
Enumeration<URL> getResources(String resourceName) { try { resourceName = normalizePath(resourceName); } catch (IOException e) { e.printStackTrace();
visitDir(normalizePath(root.getAbsolutePath()), root, resources);
public static void searchResource(String path) throws IOException { try { path = normalizePath(path); } catch (IOException e) { e.printStackTrace();