使用类加载器在java程序中运行一个可执行的jar文件



我听说您可以使用Process或ClassLoaders运行其他java文件。

我有一个可执行的jar"test.jar",它有一个名为Test的主类。

我找到了一种使用Process运行的方法。我需要知道如何使用ClassLoaders。

Process p = Runtime.getRuntime().exec("java -jar test.jar");
BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
synchronized (p) {
   p.waitFor();
}
int b=0;
while((b=bis.read()) >0){
   System.out.print((char)b);    
}

一些代码让你开始,基本上就像@Felipe说的那样。

请记住,这并不完全是一回事。您不是在生成一个新的进程,只是一个新线程;你可能会有一些巨大的课堂负荷头痛;Test.jar文件中的System.exit()之类的东西也会导致容器程序终止,依此类推

File f = new File("Test.jar");
ClassLoader cl = new URLClassLoader(new URL[] { f.toURI().toURL() }, null);
final Class<?> claxx = cl.loadClass("my.package.Test");
final Method main = claxx.getMethod("main", String[].class);
Thread th = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            main.invoke(claxx, new Object[] { new String[] { "Param1", "Param2" } });
        } catch (Throwable th) {
            // TODO
        }
    }
});
th.setContextClassLoader(cl);
th.start();
th.join();

基本方法是:

  • 创建类加载器
  • 读取清单以查找入口点类
  • 加载入口点类
  • 使用反射来获得其public static void main(String[])方法,并且
  • 从新线程调用main方法,传递该命令的命令行参数

在这种特殊情况下,问题在于设置System.in和System.out流,以便您可以与运行在同一JVM中的"子应用程序"进行对话。问题是,"子应用程序"希望System.out是它将输出写入的流,而"父应用程序"将从中读取。但是"父应用程序"希望System.out是控制台的输出流(或其他)。这是行不通的,因为System.in/out/err是两个"应用程序"不可避免地具有相同值的类字段。

解决方案是将父应用程序类的代码更改为不使用System.in/out/err字段。在"父级"调用子级上的main方法之前,必须使用System.setOut(...)将其设置为将写入缓冲区的自定义输出流实例。然后,您需要一个匹配的输入流实例,"父"可以从中读取。。。代替代码中的bis

您需要解决的另一件事是"子"应用程序将调用System.exit()的可能性。这将给父母带来不幸的后果。

还有各种各样的其他复杂性,比如清理线程和其他由"孩子"创建的东西。其中一些基本上是无法解决的,除非JVM实现Isolates JSR(主流JVM都没有实现,AFAIK)。

简言之,在一般情况下这样做真的很难,有些事情你根本无法完全正确。除非您能够消除/避免这些问题,否则实现替代入口点和替代通信方式要简单得多。

您可以使用类java.net.URLClassLoader。您必须实例化URLClassLoader来传递所需JAR的URL(可以是文件URL)。然后,您可以使用这个类加载器来加载带有main方法的类。URLClasLoader.loadClass()方法将返回表示已加载类的Class实例。这样,您就可以通过reflecton实例化它并进一步调用它。您必须直接调用main()方法。

此代码可能会帮助您:

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
public class Bootstrap {
public static void main(String[] args) throws Exception {
    /*
        Assume your application has a "home" directory
        with /classes and /lib beneath it, where you can put
        loose files and jars.
        Thus,
        /usr/local/src/APP
        /usr/local/src/APP/classes
        /usr/local/src/APP/lib
     */
    String HOME = "/usr/local/src/YOURAPP";
    String CLASSES = HOME + "/classes";
    String LIB = HOME + "/lib";
    // add the classes dir and each jar in lib to a List of URLs.
    List urls = new ArrayList();
    urls.add(new File(CLASSES).toURL());
    for (File f : new File(LIB).listFiles()) {
        urls.add(f.toURL());
    }
    // feed your URLs to a URLClassLoader!
    ClassLoader classloader =
            new URLClassLoader(
                    urls.toArray(new URL[0]),
                    ClassLoader.getSystemClassLoader().getParent());
    // relative to that classloader, find the main class
    // you want to bootstrap, which is the first cmd line arg
    Class mainClass = classloader.loadClass(args[0]);
    Method main = mainClass.getMethod("main",
            new Class[]{args.getClass()});
    // well-behaved Java packages work relative to the
    // context classloader.  Others don't (like commons-logging)
    Thread.currentThread().setContextClassLoader(classloader);
    // you want to prune the first arg because its your main class.
    // you want to pass the remaining args as the "real" args to your main
    String[] nextArgs = new String[args.length - 1];
    System.arraycopy(args, 1, nextArgs, 0, nextArgs.length);
    main.invoke(null, new Object[] { nextArgs });
}
}

从这里出发。

相关内容

最新更新