如何在Java的新流程中启动"main"? [英] How can I start a 'main' in a new process in Java?
问题描述
问题很简单.如何在另一个Java进程中启动main方法?现在,我这样做:
The question is rather simple. How can I start a main method in another java process? Now I do it like this:
startOptions = new String[] {"java", "-jar", "serverstart.jar"};
new ProcessBuilder(startOptions).start();
但是他们要求我不要使用外部.jar文件. serverstart.jar显然具有main方法,但是是否可以在另一个进程中调用该main方法,而无需调用.jar文件?
But they asked me to do it not with an external .jar file. The serverstart.jar obviously has a main method, but it it possible to call that main method in another process, without calling the .jar file?
我在想这样的事情:
new ProcessBuilder(ServerStart.main(startOptions)).start();
但是我不知道是否存在类似的东西.
But I don't know if anything like that exists.
亲切的问候,
推荐答案
假设带有新的类加载器的新线程是不够的(尽管我会为此解决方案投票),但我了解您需要创建一个不同的过程来调用类中的main方法,而在清单文件中没有声明为"jar main method"-因为您再也没有单独的serverstart.jar了.
Assuming a new thread with a new classloader is not enough (I would vote for this solution though), I understand you need to create a distinct process that invokes a main method in a class without having that declared as "jar main method" in the manifest file -- since you don't have a distinct serverstart.jar anymore.
在这种情况下,您可以简单地调用java -cp $yourClassPath your.package.ServerStart
,就像在没有(或不想使用)清单Main-Class时运行任何Java应用程序一样.
In this case, you can simply call java -cp $yourClassPath your.package.ServerStart
, as you would do for running any java application when you don't have (or don't want to use) the manifest Main-Class.
这篇关于如何在Java的新流程中启动"main"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!