如何在java中调用dos命令 100分求解

shaoang007 2004-07-08 08:48:23
最好给我一个例子 谢谢
...全文
128 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wulemale 2004-07-08
  • 打赏
  • 举报
回复

import java.io.*;
public class Test3
{
public static void main(String[] args)
{

try {
Process s = Runtime.getRuntime().exec("c:\\windows\\System32\\cmd.exe /c dir");//这个地方改为你自己的windows目录
InputStreamReader ir = new InputStreamReader(s.getInputStream());

LineNumberReader input = new LineNumberReader(ir);

String line;

while ( (line = input.readLine()) != null)
{

System.out.println(line);

}
}
catch (IOException ex)
{
System.out.print(ex.getMessage());
}
}
}
shuneng 2004-07-08
  • 打赏
  • 举报
回复
Runtime.exec("shutdown -s");
如果运行其它则要加上wait()
Acylas 2004-07-08
  • 打赏
  • 举报
回复
比如 String command = "cmd";
execute(command)

public static String execute(String command)
throws IOException, InterruptedException {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);
runtime.gc();
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null, result = "";
while ((line = input.readLine()) != null)
result += line + "\r\n";
input.close();
if (result.length() == 0)
return null;
else
return result;
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧