2

I am new to both PHP and Java. Currently I am working on a project which call java class and its methods in the PHP code. I am creating a proof of concept for this. The problem is I am not able to connect custom class I have created in java. More over it could be said I don't know how to do it. I have used java.inc and javabridge.jar files to connect to the system classes but not able to connect to a simple class. The step I have followed is:

Created a java package, class called clsForPHP and it has a method sum() which takes 2 parameters and returns integer value. (This is created using MyEclipse IDE) Now I am trying to call this function from PHP. I have copied the jar file containg the package to PHP project. (Eclipse- Helios is the IDE) $d= new java("clsForPHP.class");

Please help!! I have searched a lot but couldn't able to find a proper solution. What I think is this package should be added in the java.inc file but I don't know how to do it.

idds24
  • 45
  • 1
  • 1
  • 8
  • Why not just open a webservice for your java classes and then you should be able to use them in any language. – Odinn Apr 18 '12 at 06:40
  • See this: http://php-java-bridge.sourceforge.net/doc/how_it_works.php – web-nomad Apr 18 '12 at 06:48
  • Pushpesh, The link describes standard java class. But I want to use custom java class. I am able to connect to standard java class. – idds24 Apr 18 '12 at 09:09

1 Answers1

4

First of all install Tomcat6 and Java in you Unix box. Most probably your Tomcat will be on 8080 port. Now Download JavaBridge.war from http://php-java-bridge.sourceforge.net/pjb/ unzip it. Then from WEB-INF/lib folder copy JavaBridge.jar ,php-servlet.jar and php-script.jar in lib folder of Tomcat. Then copy JavaBridge.war in your Tomcat6 webapps folder and restart your tomcat6 service which will deploy automatically JavaBridge folder in your webapps

Now try to browse http://localhost:8080/JavaBridge/. If you get working than your PHP/Java Bridge installed. First Part is over.

Now make test page in your apache as below

<?php
      require_once("http://localhost:8080/JavaBridge/java/Java.inc");
      echo java("java.lang.System")->getProperties();
?>

and run it. If it works then you can start working with your work. If it not work then you have problem with you php.ini file. Try to make allow_url_once=on in your php.ini.

Now just copy your java jar file in tomcat /webapps/JavaBridge/WEB-INF/lib/ folder and Just always put following line in your page where ever you want java to work

require_once("http://localhost:8080/JavaBridge/java/Java.inc");
$yourObj = java("your java class");
$yourObj->yourMethod();
$yourObj->setProperty("xxx");

Hope this can help you out.

IF you have still any problem get back.

Yea it will throw error because you must not have copied your java compiled file may be your jar file in PHP / Java Bridge.

You can to do in following 2 options way

  1. If you have define your CATALINA_HOME then copy $CATALINA_HOME/webapps/JavaBridge/WEB-INF/lib
  2. If you have not define your CATALINA_HOME then copy /var/lib/tomcat6/webapps/webapps/JavaBridge/WEB-INF/lib

Path which I am telling it assumes that you have install tomcat6, may be your tomcat webapps folder may be in different path.

Milind
  • 1,855
  • 5
  • 30
  • 71
  • Is there any changes I need to do if I am using Zend Apache server? I am able to execute the `java("java.lang.System")->getProperties();` successfully. I have placed it in local phpProject folder. However, `Javav("myClass");` does not recognise. It throws "Call to undefined function Javav()". – idds24 Apr 23 '12 at 06:37
  • its my mistake: I forgot to inform I am setting this in my local system which uses Win7 as OS. Does this make any difference? I am using this javabridge for my poc and it is used only by one project (poc application). – idds24 Apr 26 '12 at 06:46
  • Than do what I say exactly in Windows 7. Also before going ahead on my instruction install Apache on ur PC – Milind Apr 27 '12 at 07:17
  • @Milind, I installed javabridge according to the your instructions successfuly, but my problem is when,it runs the php file it prints the result in the command prompt (catalina_start.bat). I want to the print the result inside the webpage.How can I fix this? – Tharu Jul 26 '15 at 14:29
  • @Tharu. Kindly can you tell me on which step your are stuck – Milind Jul 26 '15 at 16:21
  • @Milind in my test.php I wrote the code 'name("Sri lanka"); ?>' When I run it I get the result as "Welcome to Sri Lanka". But I don't get this result in wepage(localhost/test.php). Instead of that I get the out put in command prompt(catalina_start.bat). How can I fix this? I want to print the output in webpage. – Tharu Jul 26 '15 at 16:45
  • @Tharu new java(**"hello.Hello"**); is your Class in Java. I think it is not class it should be your actual class – Milind Jul 26 '15 at 17:17
  • @Milind 'hello' is the package name, and Hello is the java class.Actually it prints the correct result. But problem is it is not printing any thing in the webpage.Instead of that it prints the result in catalina_start.bat. I don;t know how to fix this. – Tharu Jul 26 '15 at 17:29
  • I tried this but getting below error : Fatal error: Call to undefined function Javav() – Jerry Abraham Apr 15 '20 at 16:42