[英]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.