/** * Locates the jar file that contains the given class. * <p> * <p> * Note that jar files are not always loaded from {@link File}, * so for diagnostics purposes {@link #jarURL(Class)} is preferrable. * * @throws IllegalArgumentException * if failed to determine. */ public static File jarFile(Class<?> clazz) throws IOException { return jarFile(classFileUrl(clazz), clazz.getName().replace('.', '/') + ".class"); }
private static File fromJarUrlToFile(String resURL) throws MalformedURLException { resURL = resURL.substring(resURL.indexOf(':') + 1, resURL.lastIndexOf('!')); // cut off "scheme:" and the file name portion return new File(decode(new URL(resURL).getPath())); }
public static File jarFile(URL resource) throws IOException { return fromJarUrlToFile(resource.toExternalForm()); }
private static void copyDockerfileDirectoryFromClasspath(Class<?> containerObject, String dockerfileLocation, File dir) throws IOException { File jar = null; try { jar = Which.jarFile(containerObject); } catch (IllegalArgumentException | IOException e) { throw new IllegalArgumentException(e); } if (jar != null && jar.isFile()) { // files are packaged into a jar/war. extract them dockerfileLocation += "/"; copyDockerfileDirectoryFromPackaged(jar, dockerfileLocation, dir); } else { // Dockerfile is not packaged into a jar file, so copy locally copyDockerfileDirectoryFromLocal(dockerfileLocation, dir); } }
String originalURL = resURL; if (resURL.startsWith("jar:file:") || resURL.startsWith("wsjar:file:")) { return fromJarUrlToFile(resURL); return new File(decode(new URL("file:/" + resURL).getPath())); return new File(decode(new URL("file:" + resURL).getPath())); return new File(decode(new URL(resURL).getPath()));
/** * @deprecated Use {@link #classFileUrl(Class)} */ @Deprecated public static URL jarURL(Class<?> clazz) throws IOException { return classFileUrl(clazz); }
/** * Decode '%HH'. */ private static String decode(String s) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == '%') { baos.write(hexToInt(s.charAt(i + 1)) * 16 + hexToInt(s.charAt(i + 2))); i += 2; continue; } baos.write(ch); } try { return new String(baos.toByteArray(), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new Error(e); // impossible } }
private static void copyDockerfileDirectoryFromClasspath(Class<?> containerObject, String dockerfileLocation, File dir) throws IOException { File jar = null; try { jar = Which.jarFile(containerObject); } catch (IllegalArgumentException | IOException e) { throw new IllegalArgumentException(e); } if (jar != null && jar.isFile()) { // files are packaged into a jar/war. extract them dockerfileLocation += "/"; copyDockerfileDirectoryFromPackaged(jar, dockerfileLocation, dir); } else { // Dockerfile is not packaged into a jar file, so copy locally copyDockerfileDirectoryFromLocal(dockerfileLocation, dir); } }
String originalURL = resURL; if (resURL.startsWith("jar:file:") || resURL.startsWith("wsjar:file:")) { return fromJarUrlToFile(resURL); return new File(decode(new URL("file:/" + resURL).getPath())); return new File(decode(new URL("file:" + resURL).getPath())); return new File(decode(new URL(resURL).getPath()));
/** * @deprecated Use {@link #classFileUrl(Class)} */ @Deprecated public static URL jarURL(Class<?> clazz) throws IOException { return classFileUrl(clazz); }
/** * Decode '%HH'. */ private static String decode(String s) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == '%') { baos.write(hexToInt(s.charAt(i + 1)) * 16 + hexToInt(s.charAt(i + 2))); i += 2; continue; } baos.write(ch); } try { return new String(baos.toByteArray(), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new Error(e); // impossible } }
/** * Locates the jar file that contains the given class. * <p> * <p> * Note that jar files are not always loaded from {@link File}, * so for diagnostics purposes {@link #jarURL(Class)} is preferrable. * * @throws IllegalArgumentException * if failed to determine. */ public static File jarFile(Class<?> clazz) throws IOException { return jarFile(classFileUrl(clazz), clazz.getName().replace('.', '/') + ".class"); }
private static File fromJarUrlToFile(String resURL) throws MalformedURLException { resURL = resURL.substring(resURL.indexOf(':') + 1, resURL.lastIndexOf('!')); // cut off "scheme:" and the file name portion return new File(decode(new URL(resURL).getPath())); }
public static File jarFile(URL resource) throws IOException { return fromJarUrlToFile(resource.toExternalForm()); }