如何从JAVA运行linux命令?

时间:2016-03-12 15:27:29

标签: java linux bash plugins runcommand

我想创建一个在后台运行bash进程的java web界面。那么,在java中如何运行linux命令,如" ls -al"或运行bash脚本。是否有任何能够在JAVA中调用OS命令的包或java插件?

全部谢谢

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

已经显示here

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