Reputation: 1124
I need to make a call of Swing Java application from PHP script in Linux. Java application use swing but its main frame is invisible, so after setting DISPLAY env. variable I was able to run it from tty2 (from terminal without x11). The problem is that I can't do the same from PHP script. I use the following snippet:
$sys = system("export DISPLAY=:0.0", $output);
$sys = system("java -jar scheduler.jar -i7.txt -q2 -a6 -s -e ", $output);
And I'm getting the following in /var/log/apache2/error.log:
Exception in thread "main" java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
at java.awt.Window.<init>(Window.java:432)
at java.awt.Frame.<init>(Frame.java:403)
at java.awt.Frame.<init>(Frame.java:368)
at javax.swing.JFrame.<init>(JFrame.java:158)
at net.sukharevd.cssw.scheduler.view.AppFrame.<init>(AppFrame.java:51)
at net.sukharevd.cssw.scheduler.Main.main(Main.java:11)
Also I tried to add -Djava.awt.headless=true parameter after java
, but without success:
Exception in thread "main" java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
at java.awt.Window.<init>(Window.java:432)
at java.awt.Frame.<init>(Frame.java:403)
at java.awt.Frame.<init>(Frame.java:368)
at javax.swing.JFrame.<init>(JFrame.java:158)
at net.sukharevd.cssw.scheduler.view.AppFrame.<init>(AppFrame.java:51)
at net.sukharevd.cssw.scheduler.Main.main(Main.java:11)
Help me to make Java application execute from PHP in the proper way.
Upvotes: 1
Views: 1518
Reputation: 1088
Dont install it on tomcat, just execute your JavaBridge.jar in your .jar folder, and do :
include("localhost:8080/Java.inc");
java_require(".");
$class = java("yourpackage.Class");
I succeded open jasper report with that
Upvotes: 0
Reputation: 10143
Well, the fact is that you are getting HeadlessException means that you are launching the application in a headless enviroment (for e.g. w/o display support).
So adding "-Djava.awt.headless=true" will do just the same.
Its even explained in the docs: http://docs.oracle.com/javase/6/docs/api/java/awt/HeadlessException.html
public class HeadlessException extends UnsupportedOperationException
Thrown when code that is dependent on a keyboard, display, or mouse is called in an environment that does not support a keyboard, display, or mouse.
So basically you have to remove any code that depends on the display (such as JFrames creation and other similar things) from the application code. After that you will be able to execute it normally w/o getting HeadlessException.
Upvotes: 3