Java Utililty Methods Jar File Load

List of utility methods to do Jar File Load

Description

The list of methods to do Jar File Load are organized into topic(s).

Method

List>loadClassesFromJar(final String jarPath, final CharSequence matchingPath)
load Classes From Jar
List<Class<?>> classes = new ArrayList<>();
try {
    JarFile jarFile = new JarFile(jarPath);
    Enumeration<JarEntry> jarEntries = jarFile.entries();
    URLClassLoader cl = URLClassLoader.newInstance(new URL[] { new URL("jar:file:" + jarPath + "!/") });
    while (jarEntries.hasMoreElements()) {
        JarEntry jarEntry = jarEntries.nextElement();
        if (jarEntry.isDirectory() || !jarEntry.toString().contains(matchingPath)
...
ClassloadClassFromJar(String jarAbsolutePath, String className)
load Class From Jar
Class result;
URL[] classLoaderUrls = new URL[] { new File(jarAbsolutePath).toURI().toURL() };
URLClassLoader child = new URLClassLoader(classLoaderUrls);
result = child.loadClass(className);
return result;
ClassLoaderloadJar(File jar)
Load a jar
java.net.URL url = jar.toURI().toURL();
return new URLClassLoader(new URL[] { url });
voidloadJar(String jarPath)
Loads a JAR file so the application can access its classes.
File f = new File(jarPath);
URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(sysloader, new Object[] { f.toURI().toURL() });
ClassLoaderloadJar(String path)
load Jar
URL url = new File(path).toURI().toURL();
URL[] urls = new URL[] { url };
return new URLClassLoader(urls);
TloadJarFile(File jarFile, String className)
Loads class className from the jar file jarFile.
URL[] urls = new URL[] { jarFile.toURI().toURL() };
URLClassLoader classLoader = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
Class<?> cls = classLoader.loadClass(className);
return (T) cls.newInstance();
voidloadJarInSystemClassLoader(File out)
load Jar In System Class Loader
try {
    URLClassLoader systemClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Method declaredMethod = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
    declaredMethod.setAccessible(true);
    declaredMethod.invoke(systemClassLoader, out.toURI().toURL());
} catch (Exception e) {
    throw new IllegalStateException(e);
voidloadJarIntoClasspath(File jarFile)
load Jar Into Classpath
try {
    URL url = jarFile.toURI().toURL();
    URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    method.setAccessible(true);
    method.invoke(classLoader, url);
} catch (Exception e) {
    System.out.println("Failed to load " + jarFile + " into classpath.");
...
File[]loadJarsFromDir(final File dir)
load Jars From Dir
final File[] jars = dir.listFiles(new FilenameFilter() {
    @Override
    public boolean accept(final File dir, final String name) {
        return name.endsWith(".jar");
});
for (int i = 0; i < jars.length; i++) {
    final JarFile jarFile = new JarFile(jars[i]);
...