Blanca Hdez
Blanca Hdez

Reputation: 3563

Execute php script

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.

Upvotes: 1

Views: 607

Answers (3)

gen_Eric
gen_Eric

Reputation: 227190

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.

Upvotes: 2

Michael Lorton
Michael Lorton

Reputation: 44376

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.

Upvotes: 2

Benjamin Dubois
Benjamin Dubois

Reputation: 961

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

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

Upvotes: 0

Related Questions