Java Jar File Load loadJarIntoClasspath(File jarFile)

Here you can find the source of loadJarIntoClasspath(File jarFile)

Description

load Jar Into Classpath

License

Apache License

Declaration

public static void loadJarIntoClasspath(File jarFile) throws Exception 

Method Source Code


//package com.java2s;
/*//ww w.  j a  va 2 s .c  om
 * Copyright 2016 Hortonworks.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

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

public class Main {
    public static void loadJarIntoClasspath(File jarFile) throws Exception {
        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.");
            throw new Exception(e);
        }
    }
}

Related

  1. loadJar(File jar)
  2. loadJar(String jarPath)
  3. loadJar(String path)
  4. loadJarFile(File jarFile, String className)
  5. loadJarInSystemClassLoader(File out)
  6. loadJarsFromDir(final File dir)