Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Calling Java Classes on Server
June 16, 2007, 12:06 pm
rate this thread
I have the Java JVM installes on my server.=20
I uncommented and proerpyl filled out the [java] section in php.ini:
java.home = "C:\Program Files\Java\jdk1.5.0_10;C:\Program =
java.library = "C:\Program =
java.library.path = "C:\InstantRails\php\extensions\;C:\Program =
I created a little php file to test it all out:
$system = new Java("java.lang.System");
echo 'Java version installed = ' . =
$system->getProperty('java.version') . "<br />";
echo 'Java vendor = ' . $system->getProperty('java.vendor') . "<br =
echo 'Running on server = ' . $system->getProperty('os.name') . "<br =
$des = new java("DES","xray");
echo $des . "<br />";
echo $x . ' = ' . $des->encrypt($x);
Which outputs as:
Java version installed = 1.5.0_10
Java vendor = Sun Microsystems Inc.
Running on server = Windows XP
Note that I have a class file C:\JavaSources\MyStuff\DES.class with an =
encrypt(String) function. Everything works fine, even when I echo out =
$des it shows as Object.
However, my last line does not output the encrypt function. Can anyone =
tell me why?
Also, if I go to run the php script a subsequent time, I get:
Fatal error: Unable to create Java Virtual Machine in =
c:\instantrails\www\javafromphp.php on line 2
unless I either reboot the computer or restart my apache web server =
under which php is running. Anyone ever call a java class from php? -R. =
Re: Calling Java Classes on Server
I've used Java with PHP before and it was a doozy to get going. To sum
it up, you have to make sure that the user under which Apache is running
has access to Java... or all users.
What I did was add the following to "/etc/profile":
Whoa whoa, wait, i just realized you are on Windows. I will leave the
stuff above as reference for people having issues with Linux!
For Windows, try the equivalent (from
. Update the PATH variable (Optional)
You can run the Java 2 SDK without setting the PATH variable, or you
can optionally set it as a convenience.
Should I set the PATH variable?
Set the PATH variable if you want to be able to conveniently run the
Java 2 SDK executables (javac.exe, java.exe, javadoc.exe, etc.) from
any directory without having to type the full path of the command.
If you don't set the PATH variable, you need to specify the full
path to the executable every time you run it, such as:
C:> \j2sdk1.4.2_<version>\bin\javac MyClass.java
It's useful to set the PATH permanently so it will persist after
How do I set the PATH permanently?
To set the PATH permanently, add the full path of the
j2sdk1.4.2_<version>\bin directory to the PATH variable. Typically
this full path looks something like C:\j2sdk1.4.2_<version>\bin. Set
the PATH as follows, according to whether you are on Microsoft
Windows NT or 98/2000/ME.
Microsoft Windows NT, 2000, and XP - To set the PATH permanently:
1. Choose Start, Settings, Control Panel, and double-click
System. On Microsoft Windows NT, select the Environment tab; on
Microsoft Windows 2000 select the Advanced tab and then
Environment Variables. Look for "Path" in the User Variables and
System Variables. If you're not sure where to add the path, add
it to the right end of the "Path" in the User Variables. A
typical value for PATH is:
Capitalization doesn't matter. Click "Set", "OK" or "Apply".
The PATH can be a series of directories separated by
semi-colons (;). Microsoft Windows looks for programs in the
PATH directories in order, from left to right. You should only
have one bin directory for a Java SDK in the path at a time
(those following the first are ignored), so if one is already
present, you can update it to j2sdk1.4.2_<version>\bin.
2. The new path takes effect in each new Command Prompt window
you open after setting the PATH variable.
here's another reference: