[英]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.我在 java 和 cygwin 的帮助下运行 shell 脚本。 When i am running my code in windows xp it works fine.当我在 windows xp 中运行我的代码时,它工作正常。 Now i am trying to run same code on windows 7 i am getting above error.现在我正在尝试在 Windows 7 上运行相同的代码,但我遇到了上述错误。
(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.为什么会发生此错误。我已为 cygwin 设置了路径(PATH=.;c:\\cygwin\\bin)
如何避免这种情况。
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.但它给出的输出为E:\\NIRAJ\\example\\app.sh
,它在我的 Java 程序中。 even i am setting up pb.directory to the path.即使我正在将 pb.directory 设置为路径。
if i check System.out.print(pb.directory());
如果我检查System.out.print(pb.directory());
it gives me output C:\\cygwin\\bin\\Test
它给了我输出C:\\cygwin\\bin\\Test
In PATH variable, you need to put cygwin's bin directory before any other Windows' paths.在PATH变量,你需要把Cygwin的bin目录下的任何其他Windows的路径之前。
Do this:做这个:
PATH=c:\cygwin\bin:RestWindowsPaths
Not that:不是那个:
PATH=RestWindowsPathVariables:c:\cygwin\bin
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
这应该打印: c:\\cygwin\\bin\\test
也使用这样的分隔符: 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.