可能重复:
我正在尝试从 JAVA 控制台程序中运行 cmd 命令,例如:
ver
然后将命令的输出返回到 JAVA 中的字符串中,例如输出:
string result = "Windows NT 5.1"
原文由 Mike 发布,翻译遵循 CC BY-SA 4.0 许可协议
可能重复:
我正在尝试从 JAVA 控制台程序中运行 cmd 命令,例如:
ver
然后将命令的输出返回到 JAVA 中的字符串中,例如输出:
string result = "Windows NT 5.1"
原文由 Mike 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以在 java 中使用 Runtime exec 从 java 代码执行 dos 命令。
根据 Senthil 在这里的回答:
Process p = Runtime.getRuntime().exec("cmd /C ver");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()),8*1024);
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
System.out.println(s.replace("[","").replace("]",""));
输出 = Microsoft Windows Version 6.1.7600
原文由 RanRag 发布,翻译遵循 CC BY-SA 4.0 许可协议
15 回答8.4k 阅读
8 回答6.2k 阅读
1 回答4k 阅读✓ 已解决
3 回答6k 阅读
3 回答2.2k 阅读✓ 已解决
2 回答3.1k 阅读
2 回答3.8k 阅读
您可以为此使用以下代码