天天看点

java 调用执行.sh文件,Java - 执行.SH文件

java 调用执行.sh文件,Java - 执行.SH文件

How would I go about executing a .SH file (this is localhost, no remote connection or anything)? I've seen lots of Runtime.exec and other things when I searched but those didn't seem to work.

This is Java 6. Also if it matters, all the SH is doing is moving two folders around.

Thanks!

解决方案

ProcessBuilder pb = new ProcessBuilder("myshell.sh", "myArg1", "myArg2");

Process p = pb.start();

BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = null;

while ((line = reader.readLine()) != null)

{

System.out.println(line);

}