1

I am building a php application. In a script, there is a call to execute a jar file:

?php
exec('java -jar simulations/simulation.jar');
?>

The problem is that this command line executes the jar file:

user@ubuntu: php execSimulation.php

but not the call from the web page. The call is made by AJAX, am I missing something??

<!-- Script to execute the simulation -->
<script type="text/javascript" src="prototype.js"></script>
        <script>

            function sendRequest() {
                new Ajax.Request("ejecutarSimulacion.php", 
                    { 
                    method: 'post', 
                    postBody: 'name='+ $F('name'),
                    onComplete: showResponse 
                    });
                }

            function showResponse(req){
            alert(req.responseText);
            }
        </script>

<form id="test" onsubmit="return false;">
                                <input type="hidden" value="<?php echo $simulacion; ?>" name="name" id="name" >
                                <input type="submit" value="<?php echo $nombre; ?>" onClick="sendRequest()">
                            </form>

When I try to print only the param I send, for instance, the alert shows it, so I am sure that the call is reaching the the server, but I don't know why the jar file is not executed. Any ideas, please?

Thanks in advance.

EDIT

Error trace:

No protocol specified
Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
    at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
    at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:62)
    at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:178)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:142)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
    at java.awt.Window.init(Window.java:385)
    at java.awt.Window.<init>(Window.java:438)
    at java.awt.Frame.<init>(Frame.java:419)
    at java.awt.Frame.<init>(Frame.java:384)
    at javax.swing.JFrame.<init>(JFrame.java:174)
    at org.opensourcephysics.tools.TranslatorTool.<init>(Unknown Source)
    at org.opensourcephysics.tools.TranslatorTool.<clinit>(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at org.opensourcephysics.display.OSPRuntime.<clinit>(Unknown Source)
    at org.opensourcephysics.controls.OSPLog.fine(Unknown Source)
    at org.opensourcephysics.tools.ResourceLoader.addSearchPath(Unknown Source)
    at _users.tanqueCalentamiento.TanqueCalentamiento.<clinit>(TanqueCalentamiento.java:18)
Could not find the main class: _users.simulation.Simulation. Program will exit.
3
  • On ubuntu there a different php.ini config files for apache and cli. Do you have safe mode enabled in your /etc/php5/apache2/php.ini or is there a value in "safe_mode_exec_dir"...? Commented Aug 17, 2011 at 18:45
  • 1
    Google claims this is the solution to the X problem: export DISPLAY=:0 (linuxquestions.org/questions/linux-general-1/…) Commented Aug 17, 2011 at 19:07
  • The solution you mentioned on your second comment, doesn't make any change. In my php.ini I can find this line: safe_mode_exec_dir = Commented Aug 17, 2011 at 19:18

3 Answers 3

2

It seems your jar requires an X server to be running.

Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.

When you run it from command line, do you have X running? If you do, this would explain why it works there, and not from PHP.

You can try to "hijack" the running X session from PHP.

exec('DISPLAY=:0 java -jar simulations/simulation.jar');

You may have to first run xhost +localhost (or xhost +) from the command line, to allow the user PHP is running as to connect to X.

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

11 Comments

22 goddamn seconds! I swear, one more bad beat like this one and I'm taking hostages.
And you really should write exec('DISPLAY=:0 java -jar simulations/simulation.jar');
I am not very sure about what X server is. I'll look for information. Anyway, exec('DISPLAY=:0 java -jar simulations/simulation.jar'); is not executiong jar file :(
X sever is GUI for linux systems. When you run it from command line, you're running it from a terminal window (you have a desktop), right?
Thanks for the explanation and for the answers. Yes, I'm using a terminal window, where it executes the jar file. But not from the php script
|
2

In the immortal words of Jamie Savage, "There's yer problem."

The Java program you're running is trying to get to your X Windows "server" (i.e., the screen) on initialization, which works when you're running from the command line but not from the headless web-server. Talk to the people who wrote org.opensourcephysics.tools.TranslatorTool about how to disable this (grossly dysfunctional) behavior.

Comments

0

Use the full path in exec() : base paths are different between the CLI SAPI and the apache one.

 <?php 
 exec('full/path/to/jar');

6 Comments

thanks for answering. I tried, but the same result: exec('java -jar /opt/lampp/htdocs/blanca/simulations/simulation.jar');
is the php file in the same folder than the webpage which loads the script ?
Yes. both of them are in /opt/lampp/htdocs/blanca/ then, the jar files in /opt/lampp/htdocs/blanca/simulations
The path to the java executable should also be made absolute. /usr/bin/jre/java or whatever it happens to be.
The jar is not ecexuted with this command: exec('/usr/lib/jvm/java-6-openjdk/jre/bin/java -jar /opt/lampp/htdocs/blanca/simulations/simulation.jar');
|

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.