2015-12-09 31 views
3

我正在执行一个进程的exec命令。我希望我的程序只有在处理完成后才能继续运行。这是我的代码:我的代码将仅在通过java完成处理后继续执行

Runtime runtime = Runtime.getRuntime(); 
Process process = runtime.exec("tools/jadx/bin/jadx.bat -d JavaProjects/"); 

//the rest of the code 
System.out.println("Process is finished"); 

我需要的代码的其余部分将这个过程完成后才能执行,因为它取决于过程输出。有任何想法吗?

回答

1

waitFor是为了这个目的:

Runtime runtime = Runtime.getRuntime(); 
Process process = runtime.exec("tools/jadx/bin/jadx.bat -d JavaProjects/"); 
int lExitCode = process.waitFor(); 
//the rest of the code 
if (lExitCode == 0) 
    System.out.println("Process was finished successfull."); 
else 
    System.out.println("Process was finished not successfull."); 
+0

当我添加waitFor,执行命令“卡住”,永远不会完成。在点击eclipse上的停止按钮之后,该过程继续并完成 –

+0

但这是您的bat文件的问题。 –

+0

你是什么意思?你认为问题出在我的bat文件上? –

2

我已经得到了答案,现在的作品!

每个进程都有输入和输出流。我的具体过程必须清空输入缓冲区才能继续运行。我添加的是以下代码:

Runtime runtime = Runtime.getRuntime(); 
Process process = runtime.exec("tools/jadx/bin/jadx.bat -d JavaProjects/"); 
out = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); 
in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
String line; 
while ((line = in.readLine()) != null) { 
    System.out.println(line); 
} 
int lExitCode = process.waitFor(); 
if (lExitCode == 0) 
    System.out.println("\n\n$$$$$ Process was finished successfully $$$$$\n\n"); 
else 
    System.out.println("\n\n$$$$$ Process was finished not successfully $$$$$\n\n"); 
相关问题