集册 Java实例教程 将Jar添加到Classpath

将Jar添加到Classpath

欢马劈雪     最近更新时间:2020-01-02 10:19:05

507
将Jar添加到Classpath
/** n o w j a v a . c o m - 时代Java 提 供 **/


//package com.nowjava;


import java.io.File;


import java.lang.reflect.Method;

import java.net.URL;

import java.net.URLClassLoader;


public class Main {

    public static void addJarToClasspath(File jarFile) {

        try {

            URL url = jarFile.toURI().toURL();

            URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader/**from nowjava**/

                    .getSystemClassLoader();

            Class<?> urlClass = URLClassLoader.class;

            Method method = urlClass.getDeclaredMethod("addURL",

                    new Class<?>[] { URL.class });

            method.setAccessible(true);

    
展开阅读全文