Java - Execute a .SH file

前端 未结 2 1299
醉梦人生
醉梦人生 2021-01-25 06:05

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 bu

相关标签:
2条回答
  • 2021-01-25 06:26

    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.

    0 讨论(0)
  • 2021-01-25 06:35

    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);
     }
    
    0 讨论(0)
提交回复
热议问题