[英]java Runtime.exec to run shell script
我使用Runtime.getRuntime()。exec()從java代碼運行shell腳本。 當我將參數作為字符串傳遞時,代碼工作正常
Runtime.getRuntime().exec("sh test.sh")
因為我必須傳遞帶空格的路徑的附加參數,所以我用String數組替換了String。
String[] cmd = {"sh test.sh", "/Path/to my/resource file"};
Runtime.getRuntime().exec(cmd)
我也嘗試過
String[] cmd = {"sh test.sh"};
Runtime.getRuntime().exec(cmd)
但他們都沒有奏效。 它拋出異常
java.io.IOException: Cannot run program "sh test.sh":
java.io.IOException: error=2, No such file or directory
為什么在作為String工作時傳遞相同的腳本文件,並且當與String數組一起使用時拋出異常。 有沒有人遇到過這個問題。 請幫助我使用字符串數組作為Runtime.exec()的arugument。 提前致謝。
第一個字符串成了命令。 沒有要執行的文件'sh test.sh'。
更改
String[] cmd = {"sh test.sh", "/Path/to my/resource file"};
至
String[] cmd = {"sh", "test.sh", "/Path/to my/resource file"};
(一般使用流程構建器API )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.