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
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 =)


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 😦
LikeLike
Are you using Mac OS?
Maybe you could find some hints in the following posts.
LikeLike
Thanks i was facing same thing.
LikeLike
you are welcome. =)
LikeLike
thanks you very much dude, it solved my problem ! you saved my ass ! 🙂
LikeLike
You are welcome. =D
LikeLike
sorry, I’m still can’t execute the java file.
the shell_exec() doesn’t return anything but empty string..
know how to solve it? thank you very much
LikeLike
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”
LikeLike
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?
LikeLike
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 .
LikeLike
“javac test.java” will compile test.java into test.class only. If you want to execute the java program you should run
Make sure you have the test.class compiled.
LikeLike