62,616
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws Exception {
String cmd = "cmd /c psexec \\\\10.67.192.212 -u china\\liuxhwfe -p xxxxxx cmd /c dir D:\\";
Process pro = Runtime.getRuntime().exec(cmd);
BufferedReader read = new BufferedReader(new InputStreamReader(pro.getInputStream(),"GBK"));
String line = null;
while((line = read.readLine()) != null){
System.out.println(line);
}
pro.destroy();
}
String cmd = "cmd /c start psexec \\\\10.67.192.212 -u china\\liuxhwfe -p xxxxxx cmd /c dir D:\\";
1.下面DOS命令可以执行,会弹出DOS界面,命令中的输出信息任然无法在java程序中获取,解决办法如下:
String cmd = "cmd /c start psexec \\\\10.67.192.212 -u china\\liuxhwfe -p xxxxxx cmd /c dir D:\\" >> D:\reuslt.txt
将结果输出到文件,然后通过程序读取文件。
2.单独的java类中运行可以,但是放到JBOSS中运行就无法打印信息的问题,是由于没有将环境变量添加到当前进程中(不知道为何JBOSS程序没有将path添加到进程中),解决方法如下:
final String[] evn = new String[]{"Path=C:\\windows\\system32;C:\\windows\\;C:\\windows\\System32\\Wbem"};
...........
String line = null;
do{
Process proc = Runtime.getRuntime().exec("cmd /c tasklist | find \"psexec.exe\"", evn);
BufferedReader rs = new BufferedReader(new InputStreamReader(proc.getInputStream(),"GBK"));
line = rs.readLine();
rs.close();
proc.destroy();
} while(line != null);
3.通过start方法运行的bat文件会单独启动一个进程,即java程序无法获知命令是否已经执行完成。我的解决办法如见2中的代码,就是不停的去查看bat中启动的进程psexec.exe如果该进程不存在了就代表命令已经执行完成。
Process proc = Runtime.getRuntime().exec("cmd /c tasklist | find \"psexec.exe\"",null,new File("C:\\WINDOWS\\system32"));
BufferedReader rs = new BufferedReader(new InputStreamReader(proc.getInputStream(),"GBK"));
String line = null;
while((line = rs.readLine()) != null){
System.out.println(line);
}
这段代码在单独的java类中运行可以,但是放到JBOSS中运行就无法打印信息了。到底为什么?String runBat = "cmd /c start "+batPath;
Process process = Runtime.getRuntime().exec(runBat);
process.waitFor();//程序在这里不等待!!!!
process.destroy();