Reputation: 43
I have a java program that opens up a .exe file. What i want to do is,
1)Run the .exe file when the java program is executed - Runs perfectly
2) When running the same code again, I want to reopen the previously executed exe.I dont want the .exe file to execute again.
EDIT: Is there a way to do the above? My only concern is the 2nd question. I have made the 1st question to work
Upvotes: 2
Views: 295
Reputation: 43
On the request of Luffy. This is the code that i had used to get the list of running process and check whether a particular exe is running or not. Hope it helps for others.
try
{
String line;
String pidInfo = "";
Process proc = Runtime.getRuntime().exec("wmic.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
OutputStreamWriter oStream = new OutputStreamWriter(proc.getOutputStream());
oStream .write("process where name='test.exe'");
oStream .flush();
oStream .close();
while ((line = input.readLine()) != null) {
pidInfo+=line;
}
input.close();
if(pidInfo.contains("test.exe"))
{
System.out.println("exe already running");
}
else
{
System.out.println("exe not running");
Process p = Runtime.getRuntime().exec(new String[] {"C:\\Program Files (x86)\\ToTestEXE\\test.exe"});
}
}
catch (IOException e)
{
e.printStackTrace();
}
Upvotes: 2