[英]Can't run shell script with ProcessBuilder
我采用了類似問題之一中的代碼:
Process p = null;
ProcessBuilder pb = new ProcessBuilder("scr.sh");
pb.directory(new File("/Users/alex/"));
p = pb.start();
Thread.sleep(TimeConst.SECOND);
並從public static main()
運行此代碼,我確實將scr.sh
文件放置在alex
文件夾下,但收到異常: Caused by: java.io.IOException: error=2, No such file or directory
我的代碼有什么問題?
我刪除了指定工作目錄的行,並將文件名替換為絕對路徑,然后行了。
為了接收echo
消息,我必須從標准輸入(?)中讀取:
final Scanner in = new Scanner(p.getInputStream());
new Thread(() -> {
while (in.hasNextLine())
System.out.println(in.nextLine());
}).start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.