/** * Retrieve the location of the currently loaded jar, or possibly null if it was compiled on the fly */ public static File get() { return get(LocationResolver.class); }
private static void log(String message) { System.err.println(prefix() + message); }
private static void visitDir(String root, File dir, Collection<String> out) { final File[] files = dir.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { visitDir(root, file, out); } out.add(file.getAbsolutePath() .replace('\\', '/') .substring(root.length() + 1)); } } }
InputStream getResourceAsStream(String resourceName) { try { resourceName = normalizePath(resourceName); } catch (IOException e) { e.printStackTrace(); searchResource(resourceName); } catch (IOException e) { e.printStackTrace();
public static void searchResource(String path) throws IOException { try { path = normalizePath(path); } catch (IOException e) { e.printStackTrace(); log("SEARCHING: \"" + path + "\""); log("FAILED: failed to find anything like"); log(" \"" + path + "\""); log(" in all classpath entries:"); log(" \"" + entry.getAbsolutePath() + "\"");
/** * 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); } }
private static ImageIcon getImage(String imageName) { ImageIcon image = null; InputStream resourceAsStream = null; try { SoftReference<ImageIcon> reference = imageCache.get(imageName); if (reference != null) { image = reference.get(); } if (image == null) { String name = IMAGE_PATH + File.separatorChar + imageName; resourceAsStream = LocationResolver.getResourceAsStream(name); image = new ImageIcon(ImageIO.read(resourceAsStream)); imageCache.put(imageName, new SoftReference<ImageIcon>(image)); } } catch (IOException e) { e.printStackTrace(); } finally { if (resourceAsStream != null) { try { resourceAsStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return image; }
URL getResource(String resourceName) { try { resourceName = normalizePath(resourceName); } catch (IOException e) { e.printStackTrace(); searchResource(resourceName); } catch (IOException e) { e.printStackTrace();
Enumeration<URL> getResources(String resourceName) { try { resourceName = normalizePath(resourceName); } catch (IOException e) { e.printStackTrace(); searchResource(resourceName);