PHP – Run a Java Program in PHP

Running a Java program in PHP just like running the shell command as i mentioned yesterday.

PHP – Run Shell Command

But i have spent a morning to verify if the Java works. =.=
Here is a sample PHP which will echo the Java version in browser.

java_version.php

<?php
        // Show whoami
        $output = shell_exec("whoami");
        echo "<strong>WHOAMI</strong>";
        echo "<hr/>";
        echo "$output<br/><br/><br/><br/>";

        // Show The Java Version Before Setting Environmental Variable
        $output = shell_exec("java -version 2>&1");
        echo "<strong>Java Version Before Setting Environmental Variable</strong>";
        echo "<hr/>";
        echo "$output<br/><br/><br/><br/>";

        // Set Enviromental Variable
        $JAVA_HOME = "/usr/local/jdk1.5.0_15";
        $PATH = "$JAVA_HOME/bin:/usr/local/bin:/usr/bin:/bin";
        putenv("JAVA_HOME=$JAVA_HOME");
        putenv("PATH=$PATH");

        // Show The Java Version After Setting Environmental Variable
        $output = shell_exec("java -version 2>&1");
        echo "<strong>Java Version After Setting Environmental Variable</strong>";
        echo "<hr/>";
        echo $output;
?>

Running Enviroment

  • Ubuntu 8.04
  • PHP 5.2.4-2ubuntu5.6
  • JDK 1.5.0_15

Result

 

You can see that the user is www-data and the default JDK is OpenJDK.

SO WHAT MAKES ME SPEND A MORNING TO VERIFY THAT?
Answer: java -version 2>&1

Now i know that the Java Version is returned in stderr… =.=

Done =)

11 thoughts on “PHP – Run a Java Program in PHP”

  1. Error when I run $output = shell_exec(“java -version 2>&1”);
    Error occurred during initialization of VM Unable to load native library: libjava.jnilib

    What shoud i do 😦

    Like

  2. this is what I tried to do:
    $output = shell_exec(“java -jar java.jar”);
    echo $output;
    but it seems return nothing.
    However, I tried to run it via command line, “java -jar java.jar” and it run normally

    ps: the java.jar just a exe file that will print “Hello world”

    Like

    1. U have to check if your webserver user (www-data in ubuntu) has the right to run the java.jar, you also need to set the $JAVA_HOME and the $PAHT env variables for that webserver user.

      Does it help?

      Like

  3. can’t i compile java program using
    filename : javaphp.php

       <?php
    
            $output = shell_exec("javac test.java");
          
            echo "<strong>WHOAMI</strong>";
            echo "";
            echo "$output";
    ?>
    

    file name : test.java

    class test {
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
    }
    

    my javaphp and test files are in same folder in /opt/lampp/htdocs/javaphp
    and i am entering the address localhost/javaphp/javaphp.php
    End up with nothing output . i already have javasetup in my computer .

    Like

    1. “javac test.java” will compile test.java into test.class only. If you want to execute the java program you should run

      java test
      

      Make sure you have the test.class compiled.

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.