Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
Sergey Alexandrovich Kryukov 11-Mar-14 13:02pm    
May I know why do you need it? No, I'm not saying this request is unreasonable, just want to know why...
—SA
Nei Sa 11-Mar-14 13:28pm    
I want to swap windows of different applications.. say suppose we have opened two different applications and we just want to change focus of one window to another window.

Thnx
Sergey Alexandrovich Kryukov 11-Mar-14 14:06pm    
It's a bad idea to have related pieces of UI in different processes. Better integrate them in one process, perhaps in different modules. This would be the better solution.
I answered on how to get the ID, please see, but this is not the best approach.
—SA
Nei Sa 11-Mar-14 13:23pm    
Because I want to switch these processes window for that task I required process id.

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


-KR
 
Share this answer
 
Comments
Shubhashish_Mandal 12-Mar-14 9:22am    
+5
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
 
Share this answer
 
Comments
Shubhashish_Mandal 12-Mar-14 9:21am    
so is this a solution?

"maybe you don't really need ID"
why you think so? OP clearly asked this.

Solution 2 is the best approach to get the process Id.
Sergey Alexandrovich Kryukov 12-Mar-14 11:35am    
Clearly asked? so what? this question, if OP really needs this, is the most important point here.
—SA
Shubhashish_Mandal 13-Mar-14 13:00pm    
"is the most important point here."
means not a solution right. then it should be in comment section
Sergey Alexandrovich Kryukov 13-Mar-14 14:28pm    
No. You are missing one simple point: I actually provided the answer to OP's question, in those references. It might not be the best, but it's there. So, aren't you raising a non-existing issue?
I repeat, the most important point is for OP to review is he really needs the id, because OP could be mistaken, and I suggesting a chance to avoid this potential mistake. This is more important than to solve the problem. If you think that the issue should be ignored, you are playing destructive role; just think about it. If I did not get you right, please explain.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900