Answer the question
In order to leave comments, you need to log in
How to start and stop a Windows process from a Java program?
Tell me, please, how can I stop the Windows process from the program?
For example, with Runtime.getRuntime() I launch the Windows Calculator. By pressing the button again, I want to close it using the destroy() method of the Process class. But it doesn't close.
btnCalculator.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (!isWinCalc){ // если калькулятор не запущен
try {
procOfCalc = Runtime.getRuntime().exec("calc"); // procOfCalc - переменная типа Process
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
isWinCalc = true;
}
else {
procOfCalc.destroy();
isWinCalc = false;
}
}
});
Answer the question
In order to leave comments, you need to log in
Such garbage (indestructibility) is similar only with calc.exe. Notepad.exe and mspaint.exe stop fine!
Why?
All figured out!
Everything turned out to be simple: the Windows calculator file is called calc.exe, and the running process is called Calculator.exe.
And this is enough for the button to work normally:
btnCalculator.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (!isWinCalc){
try {
Runtime.getRuntime().exec("C:/Windows/System32/calc.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
isWinCalc = true;
}
else {
try {
Runtime.getRuntime().exec("taskkill /F /IM Calculator.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
isWinCalc = false;
}
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question