如何使用Java获取Process Id [英] How can I get Process Id using Java

查看:545
本文介绍了如何使用Java获取Process Id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JAVA获取进程ID



假设我创建了以下两个进程



运行时run = Runtime.getRuntime();

进程pro1 = run.exec(C:\\Windows\\System32 \\ calc.exe );

处理pro2 = run.exec(C:\\Windows\\System32 \\\\
otepad.exe);



现在我想访问pro1和pro2的进程ID ...



如何访问它?我是JAVA的新手

How Can get Process Id Using JAVA

suppose I have created following two processes

Runtime run = Runtime.getRuntime();
Process pro1 = run.exec("C:\\Windows\\System32\\calc.exe");
Process pro2 = run.exec("C:\\Windows\\System32\\notepad.exe");

Now I want to access Process Id of pro1 and pro2...

How can I access it? I am new to JAVA

推荐答案

String name = ManagementFactory.getRuntimeMXBean().getName();
System.out.println("Process ID for this app = " + name);





-KR



-KR


请参阅:

http://stackoverflow.com/questions/5284139/how-do-i-find-the-process-id-pid-of-a-process-started-in-java [ ^ ],

http://stackoverflow.com/questions/4750470/how-to-get-pid-of-process-ive-just-started-within-java-program [ ^ ]。



所有这些解决方案都非常难看。一个需要列出所有进程并将其中一个进程标识为刚刚启动的进程。另一个人可以依赖私人字段,你可以通过反思访问它。



想想另一件事:也许你真的没有需要身份证?如果您解释为什么认为您需要它,我们可以为您提供更好的建议。



-SA
Please see:
http://stackoverflow.com/questions/5284139/how-do-i-find-the-process-id-pid-of-a-process-started-in-java[^],
http://stackoverflow.com/questions/4750470/how-to-get-pid-of-process-ive-just-started-within-java-program[^].

All those solutions are pretty ugly. One requires listing all processes and identifying the one of them as the one you just started. Another one can rely on private field, which you can access by reflection.

Think about one different thing: maybe you don't really need ID? If you explain the reason why do you think you need it, we can advise you something better.

—SA


这篇关于如何使用Java获取Process Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆