我在寻找解决方案在stackoverflow和谷歌这个问题,但我还没有发现任何可以帮助我解决它的固体。如何在执行运行时命令后停止java挂起
我试图用java的powershell命令执行bat文件,然后在StringBuilder中存储响应,但应用程序只是挂起。这是我做了什么
------批处理文件------------
Powershell /C "Get-WMIObject -class Win32_ComputerSystem | select username"
------ Java文件----- --------
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.*;
//import org.apache.commons.exec.CommandLine;
//import org.apache.commons.exec.DefaultExecutor;
public class Test
{
public static void main(String[] args)
{
try {
String command = "cmd /C .\\scripts\\Get-Username.bat";
System.out.println(executeCommand(command).toString());
}catch(Exception e) {
e.printStackTrace();
}
}
public static StringBuilder executeCommand(String command)
{
String line = null;
StringBuilder responseBuffer = new StringBuilder();
try {
Process process = Runtime.getRuntime().exec(command);
BufferedReader inStreamReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
while((line = inStreamReader.readLine()) != null){
//System.out.println(line);
responseBuffer.append(line + "\n");
}
process.getOutputStream().close();
}catch(IOException ex) {
ex.printStackTrace();
}
return responseBuffer;
}
}
预先感谢您
它挂在哪里?你有没有在IDE调试器中查看它? – 2014-08-31 07:49:48
@JimGarrison我不使用IDE。我使用ant和文本编辑器作为我的构建工具。因为我的笔记本电脑很古老,所以它没有足够的内存来正确运行IDE。 – user3860307 2014-08-31 07:54:58
@JimGarrson纠正我,如果我错了,据我所知,似乎不去过去“while”声明 – user3860307 2014-08-31 07:56:54