3

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

Code Hungry
  • 3,930
  • 22
  • 67
  • 95

2 Answers2

1

In PATH variable, you need to put cygwin's bin directory before any other Windows' paths.

Do this:

PATH=c:\cygwin\bin:RestWindowsPaths

Not that:

PATH=RestWindowsPathVariables:c:\cygwin\bin
thanos.a
  • 2,246
  • 3
  • 33
  • 29
0

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();
Ved
  • 8,577
  • 7
  • 36
  • 69
  • So how it will run since your app.sh is in example directory and you are expecting in it Test ? also you should run the command with cygbin.exe like this : `cygbin.exe PATH/app.sh` assuming cygbin is your executable. – Ved Feb 23 '12 at 09:16
  • i am coping all the required files to the test folder. then setting pb.directory(new File("C:\\cygwin\\bin\\Test\\")); then executing the command and it is working fine with windows xp. – Code Hungry Feb 23 '12 at 09:29
  • CYGWIN_NT-6.1-WOW64 1.7.9(0.237/5/3) – Code Hungry Feb 23 '12 at 10:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8108/discussion-between-niraj-deshmukh-and-programmer-1) – Code Hungry Feb 23 '12 at 10:16