java怎么调用shell命令

29次阅读
没有评论

共计 393 个字符,预计需要花费 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 命令。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-20发表,共计393字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)