DannyF247
DannyF247

Reputation: 638

Java - Execute a .SH file

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!

Upvotes: 1

Views: 6442

Answers (2)

Martin
Martin

Reputation: 7149

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

Upvotes: 5

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78639

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.

Upvotes: 0

Related Questions