/** * Returns the absolute uri of the Class-Path entry value as specified in <a * href="http://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Main_Attributes">JAR * File Specification</a>. Even though the specification only talks about relative urls, * absolute urls are actually supported too (for example, in Maven surefire plugin). */ @VisibleForTesting static URL getClassPathEntry(File jarFile, String path) throws MalformedURLException { return new URL(jarFile.toURI().toURL(), path); } }
public URL find(String classname) { char sep = File.separatorChar; String filename = directory + sep + classname.replace('.', sep) + ".class"; File f = new File(filename); if (f.exists()) try { return f.getCanonicalFile().toURI().toURL(); } catch (MalformedURLException e) {} catch (IOException e) {} return null; }
public static URL parseURL(String input) { try { switch (parseProtocol(input)) { case NO_PROTOCOL: return new File(input).toURI().toURL(); default: return URI.create(input).toURL(); } } catch (MalformedURLException e) { throw new IllegalArgumentException(String.format("Unable to parse source: %s", input), e); } }
public static void addJarToContextLoader(File jar) throws MalformedURLException { ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader instanceof MutableURLClassLoader) { ((MutableURLClassLoader) loader).addURL(jar.toURI().toURL()); } else { URLClassLoader newLoader = new URLClassLoader(new URL[]{jar.toURI().toURL()}, loader); Thread.currentThread().setContextClassLoader(newLoader); } }
/** * Returns the URLs in the class path specified by the {@code java.class.path} {@linkplain * System#getProperty system property}. */ @VisibleForTesting // TODO(b/65488446): Make this a public API. static ImmutableList<URL> parseJavaClassPath() { ImmutableList.Builder<URL> urls = ImmutableList.builder(); for (String entry : Splitter.on(PATH_SEPARATOR.value()).split(JAVA_CLASS_PATH.value())) { try { try { urls.add(new File(entry).toURI().toURL()); } catch (SecurityException e) { // File.toURI checks to see if the file is a directory urls.add(new URL("file", null, new File(entry).getAbsolutePath())); } } catch (MalformedURLException e) { logger.log(WARNING, "malformed classpath entry: " + entry, e); } } return urls.build(); }
/** * Returns the absolute uri of the Class-Path entry value as specified in <a * href="http://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Main_Attributes">JAR * File Specification</a>. Even though the specification only talks about relative urls, * absolute urls are actually supported too (for example, in Maven surefire plugin). */ @VisibleForTesting static URL getClassPathEntry(File jarFile, String path) throws MalformedURLException { return new URL(jarFile.toURI().toURL(), path); } }
private static URLClassLoader createClassLoader(File root) throws MalformedURLException { return new URLClassLoader( new URL[]{root.toURI().toURL()}, Thread.currentThread().getContextClassLoader()); }
/** * Creates a classloader respecting the classpath option. * * @return the classloader. */ protected synchronized ClassLoader createClassloader() { URL[] urls = classpath.stream().map(path -> { File file = new File(path); try { return file.toURI().toURL(); } catch (MalformedURLException e) { throw new IllegalStateException(e); } }).toArray(URL[]::new); return new URLClassLoader(urls, this.getClass().getClassLoader()); }
/** * Returns all provided libraries needed to run the program. */ public List<URL> getAllLibraries() { List<URL> libs = new ArrayList<URL>(this.extractedTempLibraries.size() + 1); if (jarFile != null) { libs.add(jarFile); } for (File tmpLib : this.extractedTempLibraries) { try { libs.add(tmpLib.getAbsoluteFile().toURI().toURL()); } catch (MalformedURLException e) { throw new RuntimeException("URL is invalid. This should not happen.", e); } } return libs; }
private URL expandJarAndReturnURL(JarInputStream jarStream, JarEntry entry) throws IOException { File nestedJarFile = new File(jarDir, entry.getName()); nestedJarFile.getParentFile().mkdirs(); try (FileOutputStream out = new FileOutputStream(nestedJarFile)) { IOUtils.copy(jarStream, out); } LOGGER.info("Exploded Entry {} from to {}", entry.getName(), nestedJarFile); return nestedJarFile.toURI().toURL(); }
private URL getUrlForFile(File possibleFileInSrc) { if (possibleFileInSrc.exists() && !possibleFileInSrc.isDirectory()) { try { return possibleFileInSrc.toURI().toURL(); } catch(MalformedURLException malformedURLException) { logger.error("Error in dev mode while streaming files from src dir. ", malformedURLException); } } return null; }