Configuring JavaBridge on PHP 5.2 and Apache on Linux
Steps to install Java on Linux
- Untar jdk-6u5-ea-bin-b04-linux-i586-27_sep_2007.bin
- Copy it to /var
- So you should now have a /var/jdk1.6.0_05/
- Edit /etc/profile and ~/.bash_profile and add
- PATH=$PATH:/var/jdk1.6.0_05/bin
- CLASSPATH=/var/jdk1.6.0_05/lib:.:/opt/java_classes
- export PATH CLASSPATH
Restart the system for the environment to take effect or just relogin to get the setting on your env
Do an env and you should now see the path and CLASSPATH variable pointing to /var/jdk1.6.0_05/
Test java the installation by
$ java –version
java version "1.6.0_05-ea"
Java(TM) SE Runtime Environment (build 1.6.0_05-ea-b04)
Java HotSpot(TM) Client VM (build 1.6.0_05-ea-b04, mixed mode, sharing)
Installing PHP-Java-Bridge
·
- unzip php-java-bridge_4.3.0_j2ee.zip
- run test.sh
- this will create all the files
- if you do a ps –ef you will see a process like
- /var/jdk1.6.0_05/jre/bin/java -jar /root/java-bridge/tmp/ext/JavaBridge.jar SERVLET_LOCAL:8080
- This means the server is started fine
- copy the jars to java classpath
- copy ext/* /var/jdk1.6.0_05/lib/
Testing your installation
Creating a java class
cd /opt/java_classes
Create a new Java File PHP_BRIDGE.java
//begin
public class PHP_BRIDGE{
public String show(String str){
return "Test"+str;
}
public static void main(String[] args){
}
}
#javac PHP_BRIDGE.java
Create a php file to access java class/methods
cd /var/www/html
create a new php file
#file : test.php
require_once('/tvus/vista/jb/java/Java.inc'); @java_reset(); java_require('/opt/java_classes/'); $obj1=new Java(‘PHP_BRIDGE’); echo $obj1->show('this is me'); @java_reset(); ?>
From command line try to run the same
#php-cgi test.php
#jrunscript –l php test.php
"If both these work fine then you are sure that your configuration works fine."
Common Errors The I got stuck with
protocol error: Method Not Allowed The requested method PUT is not allowed for the URL /JavaBridge/JavaBridge.phpjavabridge"Make sure the port 8080 is free and your servlet server is started on 8080 ONLY"
Undefined index: content_length java bridge Or java.lang.ArrayIndexOutOfBoundsException: 5 protocol error: , Invalid document end at col 1. Check the back end log for details. at php.java.bridge.Parser.PUSH(Parser.java:149) at php.java.bridge.Parser.parse(Parser.java:184) at php.java.bridge.Request.handleRequest(Request.java:373) at php.java.bridge.Request.handleRequests(Request.java:481) at php.java.bridge.JavaBridge.run(JavaBridge.java:213) at php.java.bridge.ThreadPool$Delegate.run(ThreadPool.java:58)The server should be started as
java -jar /var/jdk1.6.0_05/lib/JavaBridge.jar SERVLET_LOCAL:8080
and not
java -jar /var/jdk1.6.0_05/lib/JavaBridge.jar INET_LOCAL:8080
4 comments:
Hi,
I am using PHP/Java Bridge with Apache 2.x, PHP 5.2 and tomcat 5.0.24 version on windows. While trying to call my custom class, I am seeing the following error:
[o:ClassNotFoundException]:"java.lang.ClassNotFoundException: Unresolved external reference: java.lang.NoClassDefFoundError: org/hibernate/HibernateException. -- Unable to call the method, see the README section "Java platform issues" for details." at: #-6 php.java.bridge.JavaBridge.getUnresolvedExternalReferenceException(JavaBridge.java:427) #-5 php.java.bridge.JavaBridge.Invoke(JavaBridge.java:1072) #-4 php.java.bridge.Request.handleRequest(Request.java:409) #0 http://127.0.0.1:8080/JavaBridge/java/Java.inc(259): java_ExceptionProxyFactory->getProxy(5, 'java.lang.Class...', true) #1 http://127.0.0.1:8080/JavaBridge/java/Java.inc(411): java_Arg->getResult(true) #2 http://127.0.0.1:8080/JavaBridge/java/Java.inc(417): java_Client->getWrappedResult(true) #3 http://127.0.0.1:8080/JavaBridge/java/Java.inc(621): java_Client->getResult() #4 http://127.0.0.1:8080/JavaBridge/java/Java.inc(1749): java_Client->invokeMethod(4, 'getCause', Array) #5 http://127.0.0.1:8080/JavaBridge/java/Java.inc(2117): java_JavaProxy->__call('getCause', Array) #6 [internal function]: java_exception->__call('getCause', Array) #7 C:\bharti\installs\htdocs\test.php(51): java_InternalException->getCause() #8 {main}]
Can you pls suggest what could be going wrong here? I am new to Java /PHP development.
Hi,
I have the same problem. Does anybody know how this issue could be solved?
Hi Zoran,
How I solved this problem is following:
- Collect all the JARs (your own classes/3rd party Jars which your code is using for execution) and copy it to the WEB-INF/lib folder of the WAR file.
- Copy the WAR file under tomcat (J2EE server) webapps folder, if you are using the server mode and restart the server.
- In your code after java_require(java/Java.inc), write java_autoload().
- I believe java_autolad loads all thr JARs present in the lib folder of your webapp. Next time when you call your custom class function, it shudnt give you unresolved external reference since all the dependent JARs have been loaded into the memory.
Pls note - I am a newbie to web programming world. So my way of getting this to work might not be the most optimal...
Thank you Bharti
Sorry for late post. I have seen your comment right now.
I have solved this problem with Fat Jar(http://fjep.sourceforge.net/fjeptutorial.html). It provides an elegant way to pack your jar file inside one jar file.
Regards,
Zoran
Post a Comment