1

I am starting a process. And later I want to be able to destroy the said process then start it again.

Process myProcess;

try {
    myProcess = Runtime.getRuntime().exec(pathToMyProgram);
} catch (IOException e) {
    e.printStackTrace();
}

Then I do stuff. And later:

try {
    myProcess.destroy();
    myProcess = Runtime.getRuntime().exec(pathToMyProgram);
} catch (IOException e) {
    e.printStackTrace();
}

The problem is at the line

myProcess.destroy();

There is an error because myProcess can be uninitialized at this point. Also I can't do something like:

myProcess = new Process();

I know I could solve this by putting everything in a big try{} statement but is there another way ?

EDIT: I cannot do the null check either, the error is: the process might not be initialized.

But thanks to you I then tried to do Process myProcess = null; and now it works ! Thank you and sorry for the bad question.

I'm removing the process tag for relevance. And edited title.

2
  • you can do null check if (myProcess!=null) myProcess.destroy(); Commented Nov 25, 2014 at 10:17
  • Thank you for your suggestion. I cannot do this test either, because it doesn't allow me to test the potentially uninitialized process. I'm adding part this to the question. Commented Nov 25, 2014 at 10:25

1 Answer 1

4

Doing so solved my problem. Sorry for the newb question.

Process myProcess = null;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.