一般java在执行CMD命令时,通常是使用Runtime.getRuntime.exec(command)来执行的,这个方法有两种细节要注意:
1.一般执行方法,代码如下,这种方法有时执行exe时会卡在那里。
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
//一般的执行方法,有时执行exe会卡在那 stmt要执行的命令
public static void executive(String stmt) throws IOException, InterruptedException {
Runtime runtime = Runtime.getRuntime(); //获取Runtime实例
//执行命令
try {
String[] command = {"cmd", "/c", stmt};
Process process = runtime.exec(command);
// 标准输入流(必须写在 waitFor 之前)
String inStr = consumeInputStream(process.getInputStream());
// 标准错误流(必须写在 waitFor 之前)
String errStr = consumeInputStream(process.getErrorStream());
new ProcessClearStream(process.getInputStream(), "INFO").start();
new ProcessClearStream(process.getErrorStream(), "ERROR").start();
int proc = process.waitFor();
InputStream errorStream = process.getErrorStream(); //若有错误信息则输出
if (proc == 0) {
System.out.println("执行成功");
} else {
System.out.println("执行失败" + errStr);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
/**
* 消费inputstream,并返回
*/
public static String consumeInputStream(InputStream is) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is,"GBK"));
String s;
StringBuilder sb = new StringBuilder();
while ((s = br.readLine()) != null) {
System.out.println(s);
sb.append(s);
}
return sb.toString();
}
|
2.第二种方法是先生成一个缓存文件,用来缓存执行过程中输出的信息,这样在执行命令的时候不会卡。代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
//这个方法比第一个好,执行时不会卡 stmt要执行的命令
public static void aaa(String stam){
BufferedReader br = null;
try {
File file = new File("D:\\\\daemonTmp");
File tmpFile = new File("D:\\\\daemonTmp\\\\temp.tmp");//新建一个用来存储结果的缓存文件
if (!file.exists()){
file.mkdirs();
}
if(!tmpFile.exists()) {
tmpFile.createNewFile();
}
ProcessBuilder pb = new ProcessBuilder().command("cmd.exe", "/c", stam).inheritIO();
pb.redirectErrorStream(true);//这里是把控制台中的红字变成了黑字,用通常的方法其实获取不到,控制台的结果是pb.start()方法内部输出的。
pb.redirectOutput(tmpFile);//把执行结果输出。
pb.start().waitFor();//等待语句执行完成,否则可能会读不到结果。
InputStream in = new FileInputStream(tmpFile);
br= new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
br = null;
tmpFile.delete();//卸磨杀驴。
System.out.println("执行完成");
} catch (Exception e) {
e.printStackTrace();
} finally {
if(br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://www.cnblogs.com/zhangzhiyong-/p/13364870.html
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 100
-
2025-06-04 93
-
2025-06-04 104
-
2025-06-04 65
-
2025-05-29 49
热门评论

