how to install .jar file onto zend server CE and call it from a php script
-
11-03-2021 - |
質問
I have the zend javabridge working. Now I want to call a .jar file myWebTest.jar with an class of the same name. Then i want to call a function testWeb which returns a hello world string.
This in on 32bit Win 7 professional system
I added myWebTest.jar to the classpath located in /zend/zendserver/etc/java_bridge_server.ini:
[JAVA_BRIDGE_SERVER]
CLASSPATH="C:\Program Files\Zend\ZendServer\bin\javamw.jar;C:\Program Files\Zend\ZendServer\bin\myWebTest.jar;."
This path is correct.
I have restarted zend server and the code I am trying is:
$jObj = new Java("myWebTest");
// Print date through the object
print $jObj->testWeb("jim");
The log:
1 {main}
thrown in C:\Program Files\Zend\Apache2\htdocs\javaObject.php on line 4
[08-Dec-2011 11:44:48] PHP Fatal error: Call to a member function testWeb() on a non-object in C:\Program Files\Zend\Apache2\htdocs\javaObject.php on line 6
[08-Dec-2011 11:45:51] PHP Fatal error: Uncaught exception 'JavaException' with message 'Java Exception java.lang.ClassNotFoundException: myWebTest
java.lang.ClassNotFoundException: myWebTest
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
in C:\Program Files\Zend\Apache2\htdocs\javaObject.php:4
Stack trace:
#0 C:\Program Files\Zend\Apache2\htdocs\javaObject.php(4): *No Class!*->jbridge('myWebTest')
#1 {main}
thrown in C:\Program Files\Zend\Apache2\htdocs\javaObject.php on line 4
I can't find a how to on this anywhere. TIA Jim
解決
From the documentation of Java
object java(string $class_name [ , ... ])
Parameters
class_name: Class name to create
...: Additional arguments are treated as constructor parameters
Return Value: The Java object that was created, NULL otherwise
Not to be confused, actually class name
needs to be the full qualified Name
of the class. As an example, if you have a class as below:
package experiment;
public class Test {
....
}
Here class name is: Test
Fully qualified name: experiment.Test
So to instantiate this class in PHP, you need to write:
$test = new Java("experiment.Test");
Additionally according to your exception log
Java Exception java.lang.ClassNotFoundException: myWebTest
It shows that, it can not find any class named as myWebTest
in your class path. Which means either there is no class named myWebTest
in the jar file, or jar is not properly loaded.
他のヒント
The argument to Java() is the fully qualified class name of the class you want to access, not the name of the jar file. For example the php java bridge docs say,
java("java.lang.Long")->MAX_VALUE
This access the class Long
, in the package java.lang
There is some info on java packages and class names here.