Java Jar Zip File addJarsToClassPath(String jarPath)

Here you can find the source of addJarsToClassPath(String jarPath)

Description

add Jars To Class Path

License

Apache License

Declaration

public static void addJarsToClassPath(String jarPath) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class Main {
    public static void addJarsToClassPath(String jarPath) {
        if (jarPath != null) {
            File f = new File(jarPath);
            try {
                URL u = f.toURI().toURL();
                URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader
                        .getSystemClassLoader();
                Class<?> urlClass = URLClassLoader.class;
                Method method = urlClass.getDeclaredMethod("addURL",
                        new Class[] { URL.class });
                method.setAccessible(true);
                method.invoke(urlClassLoader, new Object[] { u });
            } catch (MalformedURLException e) {
                System.out.println("Invalid jarPath ... " + jarPath);
                System.out.println(e.getMessage() + ":" + e.getCause());
            } catch (NoSuchMethodException | SecurityException
                    | IllegalAccessException | IllegalArgumentException
                    | InvocationTargetException e) {
                System.out.println("Failed to load jars from : " + jarPath);
                System.out.println(e.getMessage() + ":" + e.getCause());
            }//from  www .  j av a 2 s  .  com
        }
    }
}

Related

  1. addJar(File path)
  2. addJarLibralyClassPath(Object classLoaderMakedObject, File jarPath)
  3. addJarLibralySystemClassPath(File jarPath)
  4. addJARs(File dir)
  5. addJars(File directory, boolean recursive)
  6. addJarToClasspath(File jarFile)
  7. addToJar(File source, JarOutputStream jarOutput)
  8. addToJar(JarOutputStream target, String pathInsideJar, File fentry)