如何在Java中创建进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2006035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a process in Java
提问by HH.
I would like to create a process in my application. But after looking around and from Java's API I still don't quite get it.
我想在我的应用程序中创建一个进程。但是在环顾四周并从 Java 的 API 中我仍然不太明白。
Basically I want to create a multi process application. But the new process is a class in my application.
基本上我想创建一个多进程应用程序。但是新进程是我的应用程序中的一个类。
I know some of you might ask why not create a thread? Because the class is calling a matlab code, the problem and the Java class is Here
我知道你们中的一些人可能会问为什么不创建一个线程?因为类调用的是matlab代码,所以问题和Java类是Here
Is there any way to do this?
有没有办法做到这一点?
采纳答案by Michael Borgwardt
There is only one way to create processes in Java, Runtime.exec()
- basically it allows you to start a new JVM just as you would via the command line interface.
在 Java 中创建进程的方法只有一种,Runtime.exec()
基本上它允许您像通过命令行界面一样启动新的 JVM。
回答by miku
Maybe java.lang.Processcould help here ..
也许java.lang.Process可以帮助这里..
The
ProcessBuilder.start()
andRuntime.exec
methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.
的
ProcessBuilder.start()
和Runtime.exec
方法创建本机过程并返回处理的一个子类,可用于控制该过程,并获得关于它的信息的一个实例。Process 类提供了执行进程输入、执行输出到进程、等待进程完成、检查进程退出状态以及销毁(杀死)进程的方法。
回答by Aaron Digulla
I guess you know how to create a new process. If not, see hereor here.
Now you need to run java.exe
with your current classpath. You can find this classpath in the System property java.class.path
. To locate java.exe
, look in new File( System.getProperty("java.home"), "bin")
.
现在您需要java.exe
使用当前的类路径运行。您可以在 System 属性中找到这个类路径java.class.path
。要定位java.exe
,请查看new File( System.getProperty("java.home"), "bin")
。
If you have problems with this approach, I suggest to write a wrapper script and call it with enough arguments so the code in main()
can decide which actual class to invoke.
如果您对这种方法有疑问,我建议编写一个包装脚本并使用足够的参数调用它,以便代码main()
可以决定调用哪个实际类。
回答by corazza
If you want more fine-grained control, you could use ProcessBuilder
- this class allows you to set environment variables and configure the project's pipes (stdout
, in
, err
).
如果你想要更细粒度的控制,你可以使用ProcessBuilder
- 这个类允许你设置环境变量并配置项目的管道 ( stdout
, in
, err
)。
Once you've configured it, you can call ProcessBuilder#start()
as many times as you want in order to create new processes (it returns an instance of Process
). You can change the configuration for new processes in between these calls to start()
.
配置完成后,您可以ProcessBuilder#start()
根据需要多次调用以创建新进程(它返回 的实例Process
)。您可以在这些调用之间更改新进程的配置start()
.
回答by Mason Zhang
My recommendation is to take a look at zt-exec: https://github.com/zeroturnaround/zt-exec
我的建议是看一下 zt-exec:https: //github.com/zeroturnaround/zt-exec
It wrapped java.lang.ProcessBuilder and and Apache Commons Exec, and could manage process life cycle easily.
它封装了 java.lang.ProcessBuilder 和 Apache Commons Exec,可以轻松管理进程生命周期。