现在要实现一个功能:    给一个Jar的包 A.jar 要在我们的代码中加载(不是用工具加载,而是写代码加载),用那个方法或类来实现?    要求能运行A.jar其中的方法

解决方案 »

  1.   

    用到哪个类就Class.forName()加载
      

  2.   

    如果只是要用到A.jar里的方法,没必要这样做。直接导入就行了。
    如果是要启动运行A.jar,可以通过命令来现实调用。
    Process pr = Runtime.getRuntime().exec("cmd.exe /c JAVA -jar \"" + path + "/A.jar\"");path是jar的路径。
      

  3.   

    直接import jar里的类阿,只要部署的时候把jar放到你project里能找到的地方就可以
      

  4.   

    写代码加载?我就知道Runtime.getRuntime().load("A.jar");
      

  5.   

    通过类加载器(ClassLoader),从源文件(通常是.class 或 .jar文件)获得不依赖平台的字节码,
    生成你想要的类,然后调用其中的方法
      

  6.   

    URLClassLoader 自己去写吧:import java.net.URL;
    import java.net.URLClassLoader;public class ClassLoaderTest {    public static void main(String[] args) throws Exception {
            URL[] url = { new URL("file:D:/ant/gao-util/dest/gao-jpa.jar") };
            URLClassLoader classloader = new URLClassLoader(url);        
            Class<?> clazz = classloader.loadClass("net.blogjava.frankiegao123.jpa.query.DateQuery");
            Object dateQuery = clazz.newInstance();
            System.out.println(dateQuery);
        }
    }
      

  7.   


    public static void main(String[] args) {
    try { URL url = new URL("file:///D:/workspace/test/bin/test.jar"); URLClassLoader urlCL = new URLClassLoader(new URL[] { url }); Class c = urlCL.loadClass("TestClassA"); Method m = c.getMethod("method");

    m.invoke(c.newInstance(), null); } catch (Exception e) { e.printStackTrace(); } }
      

  8.   

    public class Test2 { public void init(){
    System.out.println("2st in Test2");
    }
    }public class Test1 { public static void main(String...args) throws InterruptedException, ClassNotFoundException, MalformedURLException, InstantiationException, IllegalAccessException{
    Test2 t = new Test2();
    t.init();
    Thread.sleep(10000);
    URL url = new URL("file:/E:/eclipse3.7/workspace/DCPDM2009/bin/test2.jar");
    URLClassLoader loader = new URLClassLoader(new URL[]{url});
    Class<?> clazz = loader.loadClass("dyna.framework.client.plm2tt.Test2");
    t = (Test2)clazz.newInstance();
    t.init();
    }
    }
    在sleep期间,我把Test2的init内容改了,但两次运行结果一样,请问上面的两位,你们是怎么做的?
      

  9.   

    呃,回错地方了
    看到这个一个问题http://topic.csdn.net/u/20120214/15/aaff6b94-6b77-4b25-9723-cd4eb8114502.html?50574结果confused了