2016-03-12 140 views
-4

我想製作一個在後臺運行bash進程的java web界面。所以,在java中,我如何運行linux命令,如「ls -al」或運行bash腳本。有沒有可以在JAVA中調用操作系統命令的軟件包或java插件?如何從JAVA運行linux命令?

感謝所有

回答

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 
0

正如已經顯示的here

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