Search in sources :

Example 66 with URLClassLoader

use of java.net.URLClassLoader in project ats-framework by Axway.

the class ClasspathUtils method loadJarsFromClasspath.

/**
     * This method detects and add any JAR to loadedJarsMap
     */
private void loadJarsFromClasspath() {
    loadedJarsMap.clear();
    ClassLoader classLoader = getClass().getClassLoader();
    URL[] urls = null;
    do {
        //check if the class loader is instance of URL and cast it
        if (classLoader instanceof URLClassLoader) {
            urls = ((URLClassLoader) classLoader).getURLs();
        } else {
            // if the ClassLoader is not instance of URLClassLoader we will break the cycle and log a message
            log.info("ClassLoader " + classLoader + " is not instance of URLClassLoader, so it will skip it.");
            // if the ClassLoader is from JBoss, it is instance of BaseClassLoader,
            // we can take the ClassPath from a public method -> listResourceCache(), from JBoss-classloader.jar
            // this ClassLoader is empty, we will get the parent
            classLoader = classLoader.getParent();
            continue;
        }
        try {
            loadJarsFromManifestFile(classLoader);
        } catch (IOException ioe) {
            log.warn("MANIFEST.MF is loaded, so we will not search for duplicated jars!");
        }
        // add all jars from ClassPath to the map
        for (int i = 0; i < urls.length; i++) {
            addJarToMap(urls[i].getFile());
        }
        // get the parent classLoader
        classLoader = classLoader.getParent();
    } while (classLoader != null);
    if (loadedJarsMap.isEmpty()) {
        // jars are not found, so probably no URL ClassLoaders are found
        throw new RuntimeException("Most probrably specific server is used without URLClassLoader instances!");
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) IOException(java.io.IOException) URL(java.net.URL)

Example 67 with URLClassLoader

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

the class SceneBuilderImpl method updateCustomLibrary.

private void updateCustomLibrary() {
    final URLClassLoader oldClassLoader = myClassLoader;
    myClassLoader = createProjectContentClassLoader(myProject);
    FXMLLoader.setDefaultClassLoader(myClassLoader);
    if (oldClassLoader != null) {
        try {
            oldClassLoader.close();
        } catch (IOException e) {
            LOG.info(e);
        }
    }
    final Collection<CustomComponent> customComponents = DumbService.getInstance(myProject).runReadActionInSmartMode(this::collectCustomComponents);
    try {
        final CustomLibrary customLibrary = new CustomLibrary(myClassLoader, customComponents);
        myEditorController.setLibrary(customLibrary);
        myCustomComponents = customComponents;
    } catch (Exception e) {
        LOG.info(e);
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) IOException(java.io.IOException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 68 with URLClassLoader

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

the class JavaFxInjectPageLanguageIntention method composeUserClassLoader.

private static ClassLoader composeUserClassLoader(Project project) {
    final List<URL> urls = new ArrayList<>();
    final List<String> list = OrderEnumerator.orderEntries(project).recursively().librariesOnly().runtimeOnly().getPathsList().getPathList();
    for (String path : list) {
        try {
            urls.add(new File(FileUtil.toSystemIndependentName(path)).toURI().toURL());
        } catch (MalformedURLException e1) {
            LOG.info(e1);
        }
    }
    return new URLClassLoader(urls.toArray(new URL[urls.size()]));
}
Also used : MalformedURLException(java.net.MalformedURLException) URLClassLoader(java.net.URLClassLoader) XmlFile(com.intellij.psi.xml.XmlFile) File(java.io.File) URL(java.net.URL)

Example 69 with URLClassLoader

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

the class SnapShooter method main.

public static void main(String[] args) throws Throwable {
    int origClassPathSize = Integer.parseInt(args[0]);
    int port = Integer.parseInt(args[1]);
    ClassLoader loader = SnapShooter.class.getClassLoader();
    if (loader instanceof URLClassLoader) {
        URLClassLoader ucl = (URLClassLoader) loader;
        URL[] oldURLs = ucl.getURLs();
        URL[] newURLs = new URL[origClassPathSize];
        final int startIndex = oldURLs.length - origClassPathSize;
        System.arraycopy(oldURLs, startIndex, newURLs, 0, origClassPathSize);
        loader = new URLClassLoader(newURLs, null);
        Thread.currentThread().setContextClassLoader(loader);
    }
    final Thread thread = new Thread(new SnapShooterDaemon(port), "snapshooter");
    thread.setDaemon(true);
    thread.start();
    String mainClass = args[2];
    String[] parms = new String[args.length - 3];
    for (int j = 3; j < args.length; j++) {
        parms[j - 3] = args[j];
    }
    Method m = loader.loadClass(mainClass).getMethod("main", parms.getClass());
    try {
        ensureAccess(m);
        m.invoke(null, (Object) parms);
    } catch (InvocationTargetException ite) {
        throw ite.getTargetException();
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Method(java.lang.reflect.Method) URL(java.net.URL) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 70 with URLClassLoader

use of java.net.URLClassLoader in project nhin-d by DirectProject.

the class JamesLoader method main.

@SuppressWarnings("deprecation")
public static void main(String[] args) {
    try {
        File fl = new File("testfile");
        int idx = fl.getAbsolutePath().lastIndexOf("testfile");
        String path = fl.getAbsolutePath().substring(0, idx);
        String phoenixHome = path + "src/test/resources/james-server/";
        ArrayList<URL> classLoaderURLList = new ArrayList<URL>();
        System.setProperty("phoenix.home", phoenixHome);
        classLoaderURLList.add(new File(phoenixHome + "bin/phoenix-loader.jar").toURL());
        URL[] classLoaderURLArray = classLoaderURLList.toArray(new URL[classLoaderURLList.size()]);
        //System.setProperty( "catalina.home", "C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5");
        URLClassLoader childClassLoader = new URLClassLoader(classLoaderURLArray, JamesLoader.class.getClassLoader());
        final Class<?> mainClass = childClassLoader.loadClass("org.apache.avalon.phoenix.launcher.Main");
        final Class<?>[] paramTypes = new Class[] { args.getClass() };
        final Method method = mainClass.getMethod("main", paramTypes);
        Object main_instance = mainClass.newInstance();
        method.invoke(main_instance, new Object[] { args });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

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