[英]Catch Java Exception with PHP shell_exec
Is there any way to catch a java exception from a JAR call by a php script? 有什么方法可以通过PHP脚本从JAR调用中捕获Java异常? I have (!) to use some external JAVA libraries, which throws Exceptions.
我有(!)使用一些外部JAVA库,这会引发Exceptions。 The Question is how can i extract valuable information for developers and show some explanation to the customer?
问题是我如何为开发人员提取有价值的信息并向客户显示一些解释?
PHP Script PHP脚本
try{
$cmd = "java -jar myjar.jar";
$output = shell_exec(escapeshellcmd($cmd));
}
catch(Javaexception $e){
//do some error handling ....
}
Java Jar 爪哇罐
//....
public class Main{
public static void main(String[] args){
throw new Exception("Testexception");
}
}
//...
shell_exec
only returns a String or Null, so you can't catch the Java Exception like that, try to create your own Exception and "catch it" with a if 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();
Basic answer: not directly. 基本答案:不直接。
You can use exit statuses to communicate processing status from the Java application. 您可以使用退出状态从Java应用程序传达处理状态。
In 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);
}
}
}
In PHP you can get the exit status via return_var
in exec
call: 在PHP中,您可以通过
exec
调用中的return_var
获取退出状态:
string exec ( string $command [, array &$output [, int &$return_var ]] )
You can also write Exception details to STDOUT or file and process its content in the PHP when status code is non 0. 当状态代码为非0时,您还可以将异常详细信息写入STDOUT或文件,并在PHP中处理其内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.