我試圖從java運行此腳本shell,但它不工作。無法從java命令行運行shell腳本
我收到此錯誤信息:
Process exited with an error: 1 (Exit value: 1)
有人能幫忙嗎?
String pwd = "blabla";
String s_key = "0000";
String path = "C:/Files/scripts";
CommandLine commandLine = CommandLine.parse("C:\\Program Files (x86)\\Git\\bin\\git.exe");
commandLine.addArgument("fileName.sh");
commandLine.addArgument(password);
commandLine.addArgument(s_key);
DefaultExecutor defaultExecutor = new DefaultExecutor();
ByteArrayOutputStream sdtout = new ByteArrayOutputStream();
ByteArrayOutputStream sdterr = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(sdtout, sdterr);
defaultExecutor.setStreamHandler(streamHandler);
defaultExecutor.execute(commandLine);
這裏是腳本
#!/bin/sh
pwd=$1
s_key=$2
....
echo $pwd
它用git bash的效果很好
$ ./fileName.sh blabla 0000
nkfjWmiG7dDnYUmjr6VD0A==
您是否嘗試檢查執行程序的Exception或stderr/stdout? https://commons.apache.org/proper/commons-exec/apidocs/org/apache/commons/exec/ExecuteStreamHandler.html –
你說的「適用於git bash」,爲了執行你需要打開的shell Git Bash控制檯? o您可以直接在Windows cmd控制檯上執行shell? – reos
@reos是的,我使用git bash控制檯,我用控制檯輸出更新了問題。該腳本不能在Windows cmd控制檯上工作...感謝 – Prosp