gpt4 book ai didi

java - 如何从JAVA运行linux命令?

转载 作者:太空宇宙 更新时间:2023-11-04 06:04:27 26 4
gpt4 key购买 nike

我想制作一个在后台运行 bash 进程的 java Web 界面。那么,在java中如何运行像“ls -al”这样的linux命令或运行bash脚本。是否有任何包或java插件能够在JAVA中调用OS命令?

谢谢大家

最佳答案

正如我在这里所示:Compiling a C Source through javacode using gcc

您可以使用ProcessBuilder来做到这一点:

String directory = "/home/user";
String[] commands = {"ls", "-al"};

ProcessBuilder pb = new ProcessBuilder();
pb.directory(new File(directory));
pb.command(commands);

Process p = pb.start();

OuputStream out = p.getOutputStream(); // to send other commands/data
InputStream in = p.getInputStream(); // to read console response
// process the streams

关于java - 如何从JAVA运行linux命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35959544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com