-3

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

谢谢大家

4

2 回答 2

0

正如我在这里展示的: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
于 2016-03-12T15:39:35.143 回答
0

这里所示

您可以使用该Runtime.exec()方法。

于 2016-03-12T15:44:01.847 回答