Java - Execute a .SH file

你说的曾经没有我的故事 提交于 2019-12-02 07:11:21

问题


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!


回答1:


You could use ProcessBuilder

 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);
 }



回答2:


You may also give some consideration to the JSch library if you do not want to make your code platform-dependent by directly invoking OS commands.



来源:https://stackoverflow.com/questions/10422869/java-execute-a-sh-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!