Attempting to call Java Servlet/Server App via PHP

Discussion in 'PHP' started by 137th Gebirg, Sep 22, 2008.

  1. #1
    Hello all:

    Running into some difficulty here. I'm trying to call a java .jar file through PHP's "exec()" command-line execution process - Red Hat Linux environment. I'm successfully setting the working directory up front and the exec function returns no errors, but the java interpreter never actually gets fired off. Here's a sample code of what I'm trying to do:

    
    <?php
    $webdir = getcwd(); // Get main web root directory (should be "/var/www/html")
    $sourcedir = "$webdir/java"; // We want to start in the java directory
    chdir ($sourcedir); // Change to new java directory
    $execparam = "java -jar JavaFile.jar &"; // Call the jar file and run in the background
    exec ($execparam, $output, $return); // Run it
    foreach ($output as $line) {
      print "$line<BR>";
    }
    print "$return<BR>";
    ?>
    
    PHP:
    That's it. Nothing is being stored in "$output" and "$return" returns "0", which I understand to be a successful run, yet no Java process gets started. I've also tried the "shell_exec()" and "system()" functions with similar (no) results.

    Anything stand out to anyone on this? Any assistance appreciated.
     
    137th Gebirg, Sep 22, 2008 IP
  2. 137th Gebirg

    137th Gebirg Active Member

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #2
    Update:

    When removing the "&" command to run this thing in the background, a generic error code of "127" is returned, rather than "0".

    Additionally, when I tried using the "popen()" function, I need to specify the location of the java interpreter, but when I do, I get the following error which gives me a little more information: "sh: /usr/bin/java: Permission denied". I have since added the "apache" user to the "root" group, but that hasn't changed anything. Is this really a permissions problem or a path problem? Very confused now...
     
    137th Gebirg, Sep 22, 2008 IP
  3. mkoga

    mkoga Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If the servlet is accessible online, why not calling it via an http request?
     
    mkoga, Sep 22, 2008 IP
  4. 137th Gebirg

    137th Gebirg Active Member

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #4
    Because it has no interface. It's was strictly designed to be a command-line thing and it was decided to put a PHP front end on it later on. I've used command line executions before, but for some reason it's not behaving with Java. I ran into this situation on Windows, where I had to go into the actual Apache service preferences and click on the "Allow service to interact with desktop" checkbox, allowing PHP code to initiate a Windows client app. I get the feeling this is a similar issue, but in Linux, like there is some setting that will allow PHP to make remote command-line calls to Java.
     
    137th Gebirg, Sep 22, 2008 IP
  5. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #5
    This might help as well. Not a solution with the posters original code:

    http://us3.php.net/manual/en/java.examples.php
     
    Sillysoft, Sep 22, 2008 IP
  6. 137th Gebirg

    137th Gebirg Active Member

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #6
    Any other ideas?
     
    137th Gebirg, Sep 24, 2008 IP