Search in sources :

Example 81 with URLClassLoader

use of java.net.URLClassLoader in project L42 by ElvisResearchGroup.

the class L42 method setClassPath.

private static void setClassPath(Path p) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    assert Files.isDirectory(p);
    List<URL> fileNames = new ArrayList<>();
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(p)) {
        for (Path path : directoryStream) {
            fileNames.add(path.toUri().toURL());
        }
    } catch (IOException ex) {
        Assertions.codeNotReachable("" + ex);
    }
    //System.out.println(fileNames);
    L42.pluginPaths = fileNames;
    URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
    method.setAccessible(true);
    for (URL url : fileNames) {
        method.invoke(loader, new Object[] { url });
    }
}
Also used : Path(java.nio.file.Path) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Method(java.lang.reflect.Method) URL(java.net.URL)

Example 82 with URLClassLoader

use of java.net.URLClassLoader in project jdk8u_jdk by JetBrains.

the class NIOCharsetAvailabilityTest method addCharsets.

private static void addCharsets(Set charsets, final String packageName) throws Exception {
    String classPath = (String) java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("sun.boot.class.path"));
    String s = (String) java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("java.class.path"));
    // Search combined system and application class path
    if (s != null && s.length() != 0) {
        classPath += File.pathSeparator + s;
    }
    while (classPath != null && classPath.length() != 0) {
        int i = classPath.lastIndexOf(java.io.File.pathSeparatorChar);
        String dir = classPath.substring(i + 1);
        if (i == -1) {
            classPath = null;
        } else {
            classPath = classPath.substring(0, i);
        }
        classPathSegments.insertElementAt(dir, 0);
    }
    // add extensions from the extension class loader
    ClassLoader appLoader = Launcher.getLauncher().getClassLoader();
    URLClassLoader extLoader = (URLClassLoader) appLoader.getParent();
    URL[] urls = extLoader.getURLs();
    for (int i = 0; i < urls.length; i++) {
        try {
            URI uri = new URI(urls[i].toString());
            classPathSegments.insertElementAt(uri.getPath(), 0);
        } catch (URISyntaxException e) {
        }
    }
    String[] classList = (String[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

        public Object run() {
            return getClassList(packageName, "");
        }
    });
    for (int i = 0; i < classList.length; i++) {
        try {
            Class clazz = Class.forName(packageName + "." + classList[i]);
            Class superclazz = clazz.getSuperclass();
            while (superclazz != null && !superclazz.equals(Object.class)) {
                if (superclazz.equals(Charset.class)) {
                    charsets.add(clazz);
                    break;
                } else {
                    superclazz = superclazz.getSuperclass();
                }
            }
        } catch (ClassNotFoundException e) {
        }
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader)

Example 83 with URLClassLoader

use of java.net.URLClassLoader in project opentsdb by OpenTSDB.

the class PluginLoader method addURL.

/**
   * Attempts to add the given file/URL to the class loader
   * @param url Full path to the file to add
   * @throws SecurityException if there is a security manager present and the
   * operation is denied
   * @throws IllegalArgumentException if the path was not a directory
   * @throws NoSuchMethodException if there is an error with the class loader 
   * @throws IllegalAccessException if a security manager is present and the
   * operation was denied
   * @throws InvocationTargetException if there is an issue loading the jar
   */
private static void addURL(final URL url) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class<?> sysclass = URLClassLoader.class;
    Method method = sysclass.getDeclaredMethod("addURL", PARAMETER_TYPES);
    method.setAccessible(true);
    method.invoke(sysloader, new Object[] { url });
    LOG.debug("Successfully added JAR to class loader: " + url.getFile());
}
Also used : URLClassLoader(java.net.URLClassLoader) Method(java.lang.reflect.Method)

Example 84 with URLClassLoader

use of java.net.URLClassLoader in project intellij-community by JetBrains.

the class PluginDescriptorTest method testFilteringDuplicates.

@Test
public void testFilteringDuplicates() throws MalformedURLException {
    URL[] urls = { new File(getTestDataPath(), "duplicate1.jar").toURI().toURL(), new File(getTestDataPath(), "duplicate2.jar").toURI().toURL() };
    assertEquals(1, PluginManagerCore.testLoadDescriptorsFromClassPath(new URLClassLoader(urls, null)).size());
}
Also used : URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 85 with URLClassLoader

use of java.net.URLClassLoader in project intellij-community by JetBrains.

the class RemoteAgentClassLoaderCache method getOrCreateClassLoader.

public URLClassLoader getOrCreateClassLoader(Set<URL> libraryUrls) {
    URLClassLoader result = myUrls2ClassLoader.get(libraryUrls);
    if (result == null) {
        result = new URLClassLoader(libraryUrls.toArray(new URL[libraryUrls.size()]), null);
        myUrls2ClassLoader.put(libraryUrls, result);
    }
    return result;
}
Also used : URLClassLoader(java.net.URLClassLoader)

Aggregations

URLClassLoader (java.net.URLClassLoader)1351 URL (java.net.URL)872 File (java.io.File)514 Test (org.junit.Test)317 IOException (java.io.IOException)256 ArrayList (java.util.ArrayList)202 MalformedURLException (java.net.MalformedURLException)186 Method (java.lang.reflect.Method)177 InvocationTargetException (java.lang.reflect.InvocationTargetException)68 JarFile (java.util.jar.JarFile)54 InputStream (java.io.InputStream)50 HashSet (java.util.HashSet)49 HashMap (java.util.HashMap)44 URISyntaxException (java.net.URISyntaxException)41 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)35 Path (java.nio.file.Path)33 QuickTest (com.hazelcast.test.annotation.QuickTest)32 Test (org.junit.jupiter.api.Test)28 URI (java.net.URI)27 List (java.util.List)27