Is there any way to catch a java exception from a JAR call by a php script? I have (!) to use some external JAVA libraries, which throws Exceptions. The Question is how can i extract valuable information for developers and show some explanation to the customer?
PHP Script
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
$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.
In 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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.