集册 Java实例教程 将Jar添加到类路径

将Jar添加到类路径

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

489
将Jar添加到类路径


//package com.nowjava;
/**来自 n o w j a v a . c o m - 时  代  Java**/

import java.io.IOException;


import java.net.URL;

import java.net.URLClassLoader;


public class Main {

    public static void addJarToClassPath(URL[] urls) throws Exception {

        URLClassLoader sysloader = (URLClassLoader) ClassLoader

                .getSystemClassLoader();

        java.lang.Class<URLClassLoader> sysclass = URLClassLoader.class;

        java.lang.Class<?>[] parameters = new java.lang.Class<?>[] { URL.class };

        try {

            java.lang.reflect.Method method = sysclass.getDeclaredMethod(// 来 自 n o w j a v a . c o m - 时代Java

                    "addURL", parameters);

            method.setAccessible(true);

            for (URL url : urls) {

                method.invoke(sysloader, new Object[] { url });

            }

        } catch (Throwable t) {

            t.prin
展开阅读全文