1

I am running a exe through java runtime api

     Process process = runTime.exec("cmd.exe /c start abc.exe "+Id, null, new       File("D:/My"));

and retrieving the process id using jna like this -

    Kernel32.INSTANCE.GetProcessId((Long) f.get(process));

but the process id return is not of abc.exe but of cmd.exe ..... i need the process id of abc.exe . Do not know how to get that can anyone help.

2 Answers2

4

The process is an object of type java.lang.Process. You can get the process id of abc.exe by using the Reflection API.

Field field = process.getClass().getDeclaredField("pid");
field.setAccessible(true);
System.out.println( field.get( process ) );
Abhishek Potnis
  • 837
  • 8
  • 18
  • 1
    +1 Note: this will give the process of the `cmd.exe` which is run, but not the process id of any process it runs. – Peter Lawrey May 13 '14 at 11:13
  • This will only work under Unix-type OSes. Under Windows there is a "handle" field that you can access using a similar method, but it does not reflect the operating system PID of the launched process; however you can access it using jna as explained there: https://stackoverflow.com/a/43426878/170637 – Olivier Gérardin May 29 '18 at 14:51
1

i cant get the process id of the exe start through cmd.exe but my purpose was served by making the cmd.exe to wait till the child exe is running using following command

Process process = runTime.exec("cmd.exe /c start /wait abc.exe "+Id, null, new       File("D:/My"));

and i got the process id of the cmd.exe using jna-api