I was Able to Run shell Script using Cygwin in java. But Now I am getting following message.
Exception in thread "main" java.io.IOException: Cannot run program "D:/cygwin/bi
n/bash": CreateProcess error=193, %1 is not a valid Win32 application
How to avoid this exception?
Code
{
String cmd;
cmd = "D:/cygwin/bin/bash -c'/bin/ls -la'";
System.out.println("EXECING: " + cmd);
p = Runtime.getRuntime().exec(cmd);
in = p.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
System.out.println("OUT:");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
in = p.getErrorStream();
br = new BufferedReader(new InputStreamReader(in));
System.out.println("ERR:");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println();
}
As recommended in a comment adding
.exewould have resolved this but so did deleting thebash.???file.There was a file named
bash.???(I never found out what extension was) in the same directory as thebash.exeand theRuntime.exec()was attempting to execute it.The
bash.???must have been created at some point after a successful execution, hence it worked once and then failed.