Java代码执行CMD命令 发表于 2022-04-21 | 更新于: 2022-04-24 | 分类于 解决方案 | 本文总阅读量 次 字数统计: 111 | 阅读时长 ≈ 1 执行jps示例123456789101112131415161718192021222324252627282930313233import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class EfferentProcess { public static void main(String[] args) throws IOException, InterruptedException { run("jps"); } /** * 执行cmd命令并获取输出结果 */ public static void run(String cmd) { Runtime runtime = Runtime.getRuntime(); try { BufferedReader br = new BufferedReader( new InputStreamReader( runtime.exec(cmd).getInputStream(), "GB2312" ) ); String line; StringBuilder b = new StringBuilder(); while ((line = br.readLine()) != null) { b.append(line).append("\n"); } System.out.println(b); } catch (Exception e) { e.printStackTrace(); } }} 参考参考 ----------- 本文结束 -----------