1

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.

2
  • 2
    "Help me to make Java application execute from PHP in the proper way" Whatever the 'proper way' is, I think it would start with 'Don't launch a rich client desktop app. from PHP..'. Commented Apr 15, 2012 at 14:32
  • not really, I succeed print jasper report from website. Just dont install it on tomcat Commented Dec 20, 2013 at 10:01

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

please don't reference outdated api versions (even if nothing much changed :-)
Changed to 6th version but its still doesn't matter - that exception did not change since 1.4. And for this specific case only the description of the exception matters.
Thanks. Yesterday I deployed this script to Windows system and everything was great. After that I got rid of Swing in application, and now it works in Linux. It was painful, but now I'm happy :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.