Java Jar File Load loadJar(String jarPath)

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

Description

Loads a JAR file so the application can access its classes.

License

Apache License

Parameter

Parameter Description
jarPath the JAR file.

Exception

Parameter Description
Exception if the JAR file cannot be loaded.

Declaration

public static void loadJar(String jarPath) throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012-2014 Esri//from  w  ww  .j  a va2  s  .  com
 * 
 *  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 {
    /**
     * Loads a JAR file so the application can access its classes.
     * @param jarPath the JAR file.
     * @throws Exception if the JAR file cannot be loaded.
     */
    public static void loadJar(String jarPath) throws Exception {
        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() });
    }
}

Related

  1. loadClassesFromJar(final String jarPath, final CharSequence matchingPath)
  2. loadClassFromJar(String jarAbsolutePath, String className)
  3. loadJar(File jar)
  4. loadJar(String path)
  5. loadJarFile(File jarFile, String className)
  6. loadJarInSystemClassLoader(File out)
  7. loadJarIntoClasspath(File jarFile)