Here you can find the source of addJar(File path)
Parameter | Description |
---|---|
path | path to jar file |
@SuppressWarnings("deprecation") private static void addJar(File path)
//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(); } }