java怎么调用shell命令

avatar
作者
猴君
阅读量:1

Java中可以通过Runtime类或者ProcessBuilder类来调用shell命令。

  1. 使用Runtime类调用shell命令:
String command = "ls -l"; // 要执行的shell命令 Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec(command); // 执行命令 
  1. 使用ProcessBuilder类调用shell命令:
String command = "ls -l"; // 要执行的shell命令 ProcessBuilder builder = new ProcessBuilder(command); Process process = builder.start(); // 执行命令 

以上代码都是执行了ls -l命令,并将结果输出到控制台。你可以根据自己的需求修改要执行的shell命令。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!