List of utility methods to do Jar File Find
File | getJarFile(@Nonnull Class> clazz) get Jar File File file = getClassFile(clazz); String path = file.getPath(); final int idx = path.lastIndexOf('!'); if (idx == -1) { return null; String jarFilePath = path.substring(0, idx); if (jarFilePath.startsWith("file:\\")) { ... |
File | getJarFile(Class clazz) Obtains the jar file that contains the class in question. String resName = clazz.getName(); resName = "/" + resName.replace('.', '/') + ".class"; URL classURL = clazz.getResource(resName); if (classURL == null) return null; String jarHeader = "jar:file:"; String urlString = classURL.toString(); if (!urlString.startsWith(jarHeader)) ... |
String | getJarFile(Class clazz) get Jar File String result = null; if (clazz != null) { String me = clazz.getName().replace(".", "/") + ".class"; URL dirURL = clazz.getClassLoader().getResource(me); if (dirURL.getProtocol().equals("jar")) { result = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); return result; |
File | getJarFile(Class> clazz) Returns null or the jar-file containing the given class. URL jarUrl = (clazz.getProtectionDomain().getCodeSource() == null ? null : clazz.getProtectionDomain().getCodeSource().getLocation()); File jarFile = null; if (jarUrl != null) { jarFile = getFile(jarUrl); if (!jarFile.isFile()) { jarFile = null; return jarFile; |
JarFile | getJarFile(final ClassLoader loader) get Jar File final URL resUrl = loader.getResource(MANIFEST); if (!resUrl.getProtocol().equals("jar")) { throw new RuntimeException("Should run as jar"); final String path = resUrl.getPath(); if (!path.startsWith("file:")) { throw new RuntimeException("Unknown jar path: " + path); final String jarPath = path.substring("file:".length(), path.indexOf('!')); try { return new JarFile(URLDecoder.decode(jarPath, "UTF-8")); } catch (UnsupportedEncodingException e) { } catch (IOException e) { return null; |
String | getJarFile(String classPath) Returns the full path of the jar file that contains the running class String dir = ClassLoader.getSystemResource(classPath).toString(); try { dir = URLDecoder.decode(dir, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); dir = dir.replace('\\', '/'); if (dir.startsWith("jar:")) ... |
JarFile | getJarFileByResourceName(Class> clazz, String resourceName) Returns the jar file, where resources are located final URL jarUrl = clazz.getResource("/" + resourceName); final JarURLConnection connection = (JarURLConnection) jarUrl.openConnection(); final URL url = connection.getJarFileURL(); final File jarFile = new File(url.getFile()); return new JarFile(jarFile); |
File | getJarFolder(Class clazz) get Jar Folder File currentJar = urlToFile(clazz.getProtectionDomain().getCodeSource().getLocation());
return currentJar;
|
String | getJarLocation() Gets the path of the running jar file String path = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); if (path.endsWith(".jar")) path = path.substring(0, path.lastIndexOf("/")); return path; |
String | getJarLocation(Class classFromJar) get Jar Location return url2File(classFromJar.getProtectionDomain().getCodeSource().getLocation()).getAbsolutePath();
|