问题:java调用bat文件,bat会使用psexe.exe到远程服务器调用一个vbscript脚本,直接使用bat没有问题,但用java调用就卡住了。
public class Test {
public static void main(String[] args) throws IOException, InterruptedException {
String command = "cmd /k D:\\ProgramFiles\\workspace_eclipse\\Java_Maintenanec_System\\src\\com\\dengdie\\util\\script\\rebootwinserver\\UpdatePackages.bat";
Process proc = Runtime.getRuntime().exec(command);
proc.getOutputStream().close();
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "Error");
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "Output");
errorGobbler.start();
outputGobbler.start();
proc.waitFor();
}
}
class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (type.equals("Error"))
System.out.println(line);
else
System.out.println(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}