如何从 Java GUI 运行和终止 Fedora 中的进程
我在 Fedora 工作。我想用 Java 创建一个带有两个按钮的 GUI:开始和停止。我有两个进程:p1 和 p2。当我单击“开始”时,p1 应在后台运行,p2 应在前台运行。 (在终端中,我们通过给出 ./p1
和 ./p2
来完成此操作。)当我单击“停止”时,这两个进程都应该被终止。 (等效的终端是:CTRL+C 和kill -9 pid)。
谁能建议如何做到这一点?
I'm working in Fedora. I want to create a GUI in Java with two buttons: START and STOP. I have two processes: p1 and p2. When I click on START, p1 should run in the background and p2 should run in the foreground. (In the terminal, we do this by giving ./p1
and ./p2
.) When I click on STOP, both the processes should be killed. (The terminal equivalent is: CTRL+C and kill -9 pid).
Can anyone suggest the way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如评论中指出的,在 java 中,您可以使用 Process 和 ProcessBuilder API 来启动其他进程。
以下链接指向相关 API 文档:
http://docs.oracle.com/ javase/1.5.0/docs/api/java/lang/Process.html
http://docs.oracle.com/ javase/1.5.0/docs/api/java/lang/ProcessBuilder.html
根据 API 文档,您可以启动进程,向其传递命令行参数和环境变量,访问其输入和输出流并终止进程他们。
Stack Overflow 上还有几个相关问题可能会有所帮助,例如 这个。
As pointed out in the comments, in java you can use the Process and ProcessBuilder API's to start other processes.
The following links point to relevant API documentation:
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Process.html
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html
According to the API docs, you can start processes, pass command line arguments and environment variables to them, access their input and output streams and kill them.
There are also several related questions here on Stack Overflow that may be able to help e.g. this one.