我現在執行Java的Java程序是這樣的:殼執行Java,等到我得到的返回碼
package com.test;
public class Test {
public static void main(String[] args){
execute();
}
public static String execute(){
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "a";
}
}
我想執行在Linux shell腳本Test.execute()方法,等待直到該方法返回並獲取返回碼。但main()方法的返回是無效的,所以我能做些什麼來獲取返回碼或從中返回味精?
有什麼建議嗎?
我找到一個解決方案:
package com.test;
public class Test {
public static void main(String[] args){
execute();
}
public static String execute(){
try {
System.out.println("sleeping");;
Thread.sleep(5000);
Runtime.getRuntime().exit(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "a";
}
}
然後我的外殼:
#!/bin/bash
java -cp test.jar com.test.Test
echo "The return code of the java application is $?"
我可以得到n的值是在Runtime.getRuntime().exit(n)
;
你卡在哪個部分? – shmosel
什麼「返回碼」?你沒有設置任何上面。 –
什麼外殼?慶典? Windows cmd? –