2017-04-14 34 views
1

我想運行執行與Java類「DefaultExecutor」 shell腳本中的命令,但我得到這個錯誤:的Java不能運行程序CreateProcess的錯誤= 2

Cannot run program "get_encrypted_password.sh" (in directory "C:\Temp\scripts"): CreateProcess error=2 specified file not found". 

的腳本運行良好git bash。

有人能告訴我我在哪裏做錯了嗎?

public Entity updateWithEncryptedPassword(Entity entity) throws IOException { 
    String password = entity.getPwd(); 

    String security_key = "00000000000000000000000000000000"; 

    String path = "C:/Temp/scripts"; 

    CommandLine commandLine = CommandLine.parse("get_encrypted_password.sh"); 

    commandLine.addArgument(password); 

    commandLine.addArgument(security_key); 

    String encrypted_password = Utils.runCommandAndGetOutput(commandLine, path); 

    entity.setNewPwd(encrypted_password); 

    return super.update(entity); 
} 

public static String runCommandAndGetOutput(CommandLine commandLine, String path) throws IOException { 
    DefaultExecutor defaultExecutor = new DefaultExecutor(); 
    defaultExecutor.setExitValue(0); 
    defaultExecutor.setWorkingDirectory(new File(path)); 

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); 

    defaultExecutor.setStreamHandler(streamHandler); 

    defaultExecutor.execute(commandLine); 

    return outputStream.toString(); 
} 
+0

Java不會賦予Windows運行Unix shell腳本的能力;有一種方法可以在Windows中安裝Unix服務(Unix或Ubuntu的服務),但是該過程是Windows安裝/配置的一部分。 –

+0

@ElliottFrisch這很有趣。很高興知道未來... – finnrayment

回答

0

代替了執行 「get_encrypted_pa​​ssword.sh」 的,不能在Windows下運行,執行 「打擊」,(可能是git的慶典,),並通過 「get_encrypted_pa​​ssword.sh」 作爲參數傳遞給它,這樣的bash將執行你的腳本。

+0

雖然奇怪的是,錯誤2是「找不到文件」,我會期待一些不同的錯誤。或者,也許錯誤是「文件未找到」,因爲Windows試圖找到並執行一些「get_encrypted_pa​​ssword.sh.exe」或「get_encrypted_pa​​ssword.sh.bat」 –

+0

感謝您的回覆,我會試一試。 :) – Prosp

相關問題