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

3
  • try to run i with admin privileges. Start command line with admin privileges then run the java from command line (or start your IDE with the rights) Commented Feb 23, 2012 at 8:41
  • So test is your file or directory ? Commented Feb 23, 2012 at 8:49
  • @programmer_1: Test is my directory.inside this directory app.sh is the shell script file Commented Feb 23, 2012 at 8:52

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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();

4 Comments

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.
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.
CYGWIN_NT-6.1-WOW64 1.7.9(0.237/5/3)

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.