javacygwincygpath

java.io.IOException:Cannot run program "sh" (in directory"c:\cygwin\bin\test"):CreateProcess error=2.The system cannot find file specified


I am running shell scripts with the help of java and cygwin. When i am running my code in windows xp it works fine. Now i am trying to run same code on windows 7 i am getting above error.

(java.io.IOException)java.io.IOException:
Cannot run program "sh" (in directory"c:\cygwin\bin\test"):
CreateProcess error=2.The system cannot find file specified

Why this error occurred.I have set my path for cygwin (PATH=.;c:\cygwin\bin) How to avoid this.

ProcessBuilder pb = new ProcessBuilder ();
pb.directory(new File("C:\\cygwin\\bin\\Test\\"));
File shellfile = new File("app.sh");//File name with extension
System.out.println(shellfile.getCanonicalPath());

But it is giving the output as E:\NIRAJ\example\app.sh which is in my java program. even i am setting up pb.directory to the path.

if i check System.out.print(pb.directory()); it gives me output C:\cygwin\bin\Test


Solution

  • First try to get the path of specified file first to ensure it:

    I am not much sure but this may lead you one step ahead :

    File file = new File("app.sh");//File name with extension
    System.out.println(file.getCanonicalPath());
    

    This should print : c:\cygwin\bin\test Also use separator like this instead : c:\\cygwin\\bin\\test

    Hope this helps.

    UPDATE

    String myCommand = "c:\\cygwin\\bin\\test\\cygbin";
    String myArg = PATH_TO_shellscript+"app.sh";
    ProcessBuilder p = new ProcessBuilder(myCommand, myArg).start();