集册 Java实例教程 追加到类路径

追加到类路径

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

398
追加到类路径

/*******************************************************************************

 * CopyRight (c) 2005-2011 GLOBE Co, Ltd. All rights reserved.

 * Filename:    JVMUtil.java

 * Creator:     joe

 * Create-Date: 2011-4-27 ????10:39:08

 *******************************************************************************/

//package com.nowjava;/*来自 N o w  J a v a  .   c o m*/


import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;


public class Main {

    public static boolean appendtoClassPath(String name) {

        // ??? JDK 1.6

        // from JDK DOC "java.lang.instrument Interface Instrumentation"

        // ...

        // The system class loader supports adding a JAR file to be searched

        // if it implements a method named appendToClassPathForInstrumentation

        // which takes a single parameter of type java.lang.String.

        // The method is not required to have public access. The name of the JAR

        // file

        // is obtained by invoking the getName() method on the jarfile and this

        // is

        // provided as the parameter to the appendtoClassPathForInstrumentation

        // method.

        // ...
        /*
        nowjava.com 提 供
        */


        try {

            ClassLoader clsLoader = ClassLoader.getSystemClassLoader();

            Method appendToClassPathMethod = clsLoader.getClass()

                    .getDeclaredMethod(

                            "appendToClassPathForInstrumentation",

                            String.class);

            if (null != appendToClassPathMethod) {

                appendToClassPathMethod.setAccessible(true);

                appendToClassPathMethod.invoke(clsLoader, name);

            }

            return true;

        } catch (SecurityException e) {

            e.printStackTrace();

        } catch (
展开阅读全文