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.
Blanca Hdez
  • 3,513
  • 19
  • 71
  • 93
  • 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"...? – madflow Aug 17 '11 at 18:45
  • 1
    Google claims this is the solution to the X problem: export DISPLAY=:0 (http://www.linuxquestions.org/questions/linux-general-1/cant-connect-to-x11-window-server-using-0-0-as-the-value-of-the-display-variable-178234/) – madflow Aug 17 '11 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 = – Blanca Hdez Aug 17 '11 at 19:18

3 Answers3

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.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 1
    22 goddamn seconds! I swear, one more bad beat like this one and I'm taking hostages. – Michael Lorton Aug 17 '11 at 19:14
  • 1
    And you really should write `exec('DISPLAY=:0 java -jar simulations/simulation.jar');` – Michael Lorton Aug 17 '11 at 19:14
  • 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 :( – Blanca Hdez Aug 17 '11 at 19:25
  • 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? – gen_Eric Aug 17 '11 at 19:27
  • 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 – Blanca Hdez Aug 17 '11 at 19:29
  • Now the error says: Can't connect to X11 window server using ':0' as the value of the DISPLAY variable. Which is the right value? – Blanca Hdez Aug 17 '11 at 19:30
  • mujeresponja@ubuntu:~$ xhost + access control disabled, clients can connect from any host – Blanca Hdez Aug 17 '11 at 19:34
  • @Rocket let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2587/discussion-between-blanca-hdez-and-rocket) – Blanca Hdez Aug 17 '11 at 19:36
  • Would I make things in a different way on Windows?? I am running the same process and it stays blocked: I tryed to remove 'DISPLAY=:0', try to get an error trace, but the process is just blocked. Thanks again – Blanca Hdez Sep 03 '11 at 12:09
  • I don't know how do to this on Windows, sorry. – gen_Eric Sep 03 '11 at 20:29
  • Just to let you know: in Windows is necessary to start/stop xampp server as administrator – Blanca Hdez Sep 08 '11 at 05:04
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.

Michael Lorton
  • 43,060
  • 26
  • 103
  • 144
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');
Benjamin Dubois
  • 971
  • 7
  • 11