2

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();
}
9
  • Try adding .exe onto the end of the file name? Commented Dec 6, 2011 at 13:20
  • Could you post the Java code that attempts to run bash. Commented Dec 6, 2011 at 13:21
  • Does D:/cygwin/bin/bash definitely exist? I have cygwin and ran this fine, with exception of changing the path to bash to "C:/bin/bash" and the -c argument to "-c /bin/ls -la". Commented Dec 6, 2011 at 13:39
  • Yes D:/cygwin/bin/bash.exe is present in the path Commented Dec 6, 2011 at 13:42
  • If you type D:\cygwin\bin\bash.exe -c "/bin/ls -la" from a DOS prompt does it work? Commented Dec 6, 2011 at 13:48

1 Answer 1

1

As recommended in a comment adding .exe would have resolved this but so did deleting the bash.??? file.

There was a file named bash.??? (I never found out what extension was) in the same directory as the bash.exe and the Runtime.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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.