Issue
Im trying to execute shell script in JAVA on windows machine . below is the code
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
String abc;
String abc1;
Process proc = Runtime.getRuntime().exec("bash.exe C:\\Users\\abc\\Downloads\\test.sh");
proc.waitFor();
System.out.println(proc.exitValue());
InputStream st1 = proc.getInputStream();
BufferedReader red1 = new BufferedReader(new InputStreamReader(st1));
while ((abc = red1.readLine()) != null) {
System.out.println("Stdout1: " + abc);
if (abc != null) {
abc1 = abc;
}
}
}
the shell script is simple below
#!/bin/bash
echo "hi"
however, the exit value of script execution is coming as 127 and not the zero I followed the below posts as well but no success. tried with git.exe as well , still error . please suggest
Execute bash script in eclipse
https://unix.stackexchange.com/questions/27054/bin-bash-no-such-file-or-directory
Solution
Make sure bash.exe is in PATH
as Charles Duffy said. Having bash installed without that being the case, is, after all, not much use. When that's the case and your script is in the current directory, the following is equivalent to your code:
public class Test {
public static void main(String[] args) throws Exception {
String[] command = { "bash", "test.sh" };
ProcessBuilder pb = new ProcessBuilder(command).inheritIO();
Process p = pb.start();
p.waitFor();
System.out.println(p.exitValue());
}
}
Answered By - g00se Answer Checked By - David Marino (WPSolving Volunteer)