2012-02-23 69 views
1

我運行shell腳本的幫助下java和cygwin。當我在Windows XP中運行我的代碼時,它工作正常。現在我試圖在Windows 7上運行相同的代碼,我得到的錯誤以上。產生java.io.IOException:不能運行程序「SH」(在目錄「C: cygwin的斌測試」):CreateProcess的錯誤= 2,系統找不到指定文件

(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 

爲什麼這個錯誤occurred.I已設置我的路徑的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()); 

但它給出的輸出爲E:\NIRAJ\example\app.sh這是在我的Java程序。即使我將pb.directory設置爲路徑。

如果我檢查System.out.print(pb.directory());它給了我輸出C:\cygwin\bin\Test

+0

嘗試以管理員權限運行我。啓動命令行用管理員權限,然後運行命令行的Java(或先從您的權利IDE) – Hurda 2012-02-23 08:41:48

+0

因此,測試您的文件或目錄? – Ved 2012-02-23 08:49:52

+0

@ programmer_1:測試是我directory.inside這個目錄app.sh是shell腳本文件 – 2012-02-23 08:52:25

回答

0

首先嚐試獲得指定文件的路徑首先要保證它:

我沒有太多的肯定,但這樣可能會導致您領先一步:

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

這應該打印:c:\cygwin\bin\test 還可以使用分離器這樣的代替:c:\\cygwin\\bin\\test

希望這有助於。

UPDATE

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

我已經更新了我的問題... – 2012-02-23 09:08:11

+0

它因此,如何運行,因爲你app.sh是例子目錄你期待在它測試?你也應該用cygbin.exe運行命令:'cygbin.exe PATH/app.sh'假設cygbin是你的可執行文件。 – Ved 2012-02-23 09:16:57

+0

查看更新後的答案是否值得。 – Ved 2012-02-23 09:22:47

0

您有任何Windows路徑聲明之前的cygwin的bin目錄添加到PATH環境變量。

PATH=c:\cygwin\bin:RestWindowsPathVariables 
相關問題