我怎样才能开始一个“主要”?在Java的新进程中?
问题很简单。如何在另一个java进程中启动main方法?现在我这样做:
startOptions = new String[] {"java", "-jar", "serverstart.jar"};
new ProcessBuilder(startOptions).start();
但他们要求我不要使用外部 .jar 文件。 serverstart.jar 显然有一个 main 方法,但是是否可以在另一个进程中调用该 main 方法,而不调用 .jar 文件?
我在想这样的事情:
new ProcessBuilder(ServerStart.main(startOptions)).start();
但我不知道是否存在这样的事情。
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();
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?
I'm thinking of something like this:
new ProcessBuilder(ServerStart.main(startOptions)).start();
But I don't know if anything like that exists.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从 java 创建一个新的“java”进程是不可能的,因为两个进程不能共享一个 JVM。 (请参阅此问题和接受的答案)。
如果您可以接受创建新的
Thread
而不是Process
,您可以使用自定义的ClassLoader
来完成。这是接近新流程的地方。所有静态和最终字段都将被重新初始化!另请注意,
"ServerStart
类(对于下面的示例)必须位于当前执行 JVM 的类路径中):这是自定义类加载器:
Creating a new "java" process from java is not possible since two processes can't share one JVM. (See this question and the accepted answer).
If you can live with creating a new
Thread
instead of aProcess
you can do it with a customClassLoader
. It is as close you can get to a new process. All static and final fields will be reinitialized!Also note that the
"ServerStart
class (for the example below) must be in the class path of the current executing JVM):And this is the custom class loader:
假设带有新类加载器的新线程还不够(不过我会投票支持这个解决方案),我理解您需要创建一个不同的进程来调用类中的 main 方法,而无需在类中将其声明为“jar main 方法”清单文件——因为你不再有一个独特的 serverstart.jar 了。
在这种情况下,您可以简单地调用 java -cp $yourClassPath your.package.ServerStart ,就像您在没有(或不想使用)时运行任何 java 应用程序一样明显的主类。
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.
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 调用 shellscript 并使用它来启动新进程(如果您根本无法忍受另一个线程)。
I would suggest invoking a shellscript from java and using it to start the new process (if you cant live with just another thread at all).
我将在这里回答如何在没有 spring 的情况下创建多进程应用程序:)。
使用 spring,您可以通过 xml 配置来做到这一点。
多线程是另一个故事,这是多进程
创建一个 JavaProces 类,如下所示。您可以在您的环境中存储此类的对应方 XML/JSON。然后使用
Runtime.getRuntime().exec(processRunnerString);
启动您的进程,您应该首先找到
java.exe
、vm args
,然后分别设置-classpath
,然后设置mainClass
和args
。您可以使用
JMX
与其他进程通信。从 java.home 环境确定 java.exe 的位置。多变的
I'll answer here how to create multi process application without spring :).
With spring you can do this by xml config.
Multithread is another story, this is multi-process
Create a JavaProces class as seen below. You can store a counterparter XML/JSON of this class in your environment. Then start your process with
Runtime.getRuntime().exec(processRunnerString);
,You should first find
java.exe
,vm args
, then set-classpath
thenmainClass
andargs
respectively.You can use
JMX
to communicate with other process.Determine location of java.exe from java.home env. variable
您可以使用反射(java.lang.reflect 包)来完成此操作。
You can do this using Reflection (java.lang.reflect package).