Java Jar Zip File addJar(File path)

Here you can find the source of addJar(File path)

Description

Add a file or directory to the classpath.

License

Apache License

Parameter

Parameter Description
path path to jar file

Declaration

@SuppressWarnings("deprecation")
private static void addJar(File path) 

Method Source Code


//package com.java2s;
/* Copyright 2015 Eric Evans <eevans@wikimedia.org> and Wikimedia Foundation
 * //from   www .  j  a  v  a 2  s .  c o m
 * 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 {
    /**
     * Add a file or directory to the classpath.
     * 
     * @param path
     *            path to jar file
     */
    @SuppressWarnings("deprecation")
    private static void addJar(File path) {
        try {
            Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
            method.setAccessible(true);
            method.invoke(classLoader(), new Object[] { path.toURL() });
        } catch (Throwable e) {
            throw new RuntimeException(String.format("Error adding %s to classpath", path), e);
        }
    }

    private static URLClassLoader classLoader() {
        return (URLClassLoader) ClassLoader.getSystemClassLoader();
    }
}

Related

  1. addFileToJar(File baseDir, File src, JarOutputStream zOut)
  2. addFileToJar(File file, ZipOutputStream zipStream, String path)
  3. addFileToJar(File fileOrDirectoryToAdd, File extractedJarPath, JarOutputStream target)
  4. addFileToJar(JarOutputStream jar, InputStream file, String path)
  5. addJarLibralyClassPath(Object classLoaderMakedObject, File jarPath)
  6. addJarLibralySystemClassPath(File jarPath)
  7. addJARs(File dir)
  8. addJars(File directory, boolean recursive)