[英]Catch Java Exception with PHP shell_exec
有什么方法可以通過PHP腳本從JAR調用中捕獲Java異常? 我有(!)使用一些外部JAVA庫,這會引發Exceptions。 問題是我如何為開發人員提取有價值的信息並向客戶顯示一些解釋?
PHP腳本
try{
$cmd = "java -jar myjar.jar";
$output = shell_exec(escapeshellcmd($cmd));
}
catch(Javaexception $e){
//do some error handling ....
}
爪哇罐
//....
public class Main{
public static void main(String[] args){
throw new Exception("Testexception");
}
}
//...
shell_exec
僅返回String或Null,因此您無法捕獲到此類Java Exception,請嘗試創建自己的Exception並使用if將其“捕獲”
$cmd = "java -jar myjar.jar";
$output = shell_exec(escapeshellcmd($cmd));
if($output == 'expected output') throw new MyException();
基本答案:不直接。
您可以使用退出狀態從Java應用程序傳達處理狀態。
在Java中:
public class Main{
public static void main(String[] args){
try {
// work
} catch (MyException e) {
// handle error
// write exception to file or STDOUT
System.exit(1);
}
}
}
在PHP中,您可以通過exec
調用中的return_var
獲取退出狀態:
string exec ( string $command [, array &$output [, int &$return_var ]] )
當狀態代碼為非0時,您還可以將異常詳細信息寫入STDOUT或文件,並在PHP中處理其內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.