2017-04-14 35 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

相关问题