I am trying to execute a bash script that gets passed 4 arguments from java. I can execute the script without the arguments perfectly using this code:
try
{
String command = "bash bash/testbash.sh";
Runtime run = Runtime.getRuntime();
Process pr = run.exec(command);
pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line=buf.readLine())!=null)
{
System.out.println(line);
}
}
catch (Exception e)
{
System.out.println("Exception " + e);
e.printStackTrace();
}
}
So I was going to make a String[] to pass both the commands and the arguments to Runtime.exec(), like so:
try
{
String[] command ={"bash bash/testbash.sh"};
Runtime run = Runtime.getRuntime();
Process pr = run.exec(command);
pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line=buf.readLine())!=null)
{
System.out.println(line);
}
}
catch (Exception e)
{
System.out.println("Exception " + e);
e.printStackTrace();
}
}
which gives me this error:
Exception java.io.IOException: Cannot run program "bash bash/testbash.sh": error=2, No such file or directory
java.io.IOException: Cannot run program "bash bash/testbash.sh": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:483)
at bashExecuter.main(bashExecuter.java:43)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 3 more
this makes no sense to me. The bash file clearly exists because I just used it with the String command, but when I use the same thing with String[] command its not there? How would I pass the command with arguments?