简体   繁体   English

初始化Java变量

[英]Initializing Java variable

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. 存在错误,因为此时myProcess可以未初始化。 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 ? 我知道我可以通过将所有内容放在大try{}语句中来解决此问题,但是还有另一种方法吗?

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; 但是多亏了您,我才尝试做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. 并编辑标题。

Doing so solved my problem. 这样做解决了我的问题。 Sorry for the newb question. 对不起,新问题。

Process myProcess = null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM