1

I need to run some java code on php so I found this

http://php-java-bridge.sourceforge.net/pjb/index.php

so I downloaded tomcat and put the JavaBridge.war in the webapps directory and I got the examples working (It took me a while to figure this out)

I can even load built-in java stuff Java("java.lang.System") and run the example jars with my own PHP code.

What I couldn't do was make my own jar and run it I tried a variety of different compilers (Eclipse,Netbeans,command-line) but this didn't help.

I'm trying to do something like this http://www.developer.com/java/other/php-with-java-using-php-java-bridge-tutorial.html

Here is my java code:

package m1sk;

/**
 *
 * @author M1sk
 */
public class Hello implements java.io.Serializable{

    String hi() {return "This is my hello message";}
    String hello(String name) {return "Hello" + name;}
} 

php code:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        require_once ("java/Java.inc");
    //  $t1 = new Java("java.lang.System"); 
    //echo $t1->getProperties(); //this works
        $world = new java("m1sk.Hello");
      //  echo $world->hi();
      echo "Hello Working Thingy\n\n";
        ?>
    </body>
</html>

i put the jar in ...\tomcat\webapps\JavaBridge\WEB-INF\lib and the php file in ...\tomcat\webapps\JavaBridge

and i get the following error when i test http://localhost:8080/JavaBridge/mytest.php:

javax.servlet.ServletException: java.lang.RuntimeException: PHP Fatal error:  Uncaught [[o:Exception]:"java.lang.Exception: CreateInstance failed: new m1sk.Hello. Cause: java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class m1sk.Hello) VM: 1.5.0_20@http://java.sun.com/" at:
#-13 org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1854)
#-12 org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:890)
#-11 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1354)
#-10 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
#-9 java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
#-8 java.lang.Class.forName0(Native Method)
#-7 java.lang.Class.forName(Class.java:242)
#-6 php.java.bridge.Util.classForName(Util.java:1518)
#-5 php.java.bridge.JavaBridge.CreateObject(JavaBridge.java:445)
#-4 php.java.bridge.Request.handleRequest(Request.java:458)
#-3 php.java.bridge.Request.handleRequests(Request.java:500)
#-2 php.java.bridge.http.C in C:\xampp\tomcat\webapps\JavaBridge\java\Java.inc on line 195

    php.java.servlet.fastcgi.FastCGIServlet.handle(FastCGIServlet.java:499)
    php.java.servlet.fastcgi.FastCGIServlet.doGet(FastCGIServlet.java:521)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    php.java.servlet.PhpCGIFilter.doFilter(PhpCGIFilter.java:126)

I search the stack found this for my error: What is the reason for UnsupportedClassVersionError?

C:\Users\M1sk\Desktop>java -version
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b22)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)

C:\Users\M1sk\Desktop>javac -version
javac 1.6.0_25

the version are different but this should still work so I don't believe this is my problem

4
  • Which version of Java is tomcat referring? Commented Jun 8, 2012 at 7:21
  • @subirkumarsao i tried some stuff but couldn't get a good answer, how could i figure this? Commented Jun 8, 2012 at 8:20
  • Normally we specify Java to tomcat through JAVA_HOME or JRE_HOME environment variables. Check those if not present specify one with correct version of Java. Keep the compiler(jdk) and JVM(jre) version same, though its not a must. Commented Jun 8, 2012 at 9:05
  • @subirkumarsao I tried but then I get this C:\xampp\tomcat\bin>version.bat Using CATALINA_BASE: C:\xampp\tomcat Using CATALINA_HOME: C:\xampp\tomcat Using CATALINA_TMPDIR: C:\xampp\tomcat\temp Using JRE_HOME: C:\xampp\tomcat\jre C:\xampp\tomcat\bin>echo %JRE_HOME% C:\Program Files\Java\jre7\bi Commented Jun 9, 2012 at 20:17

1 Answer 1

1

Reason for UnsupportedClassVersionError is because your class file was compiled using a later version of the JDK (eg. Java 7) while your JRE is an earlier version (eg. Java 6).

If you are using external libraries, then it is likely that the external library was compiled with a more recent version of the Java Compiler, as compared to the JRE you are using. I would recommend you upgrade your JRE to the latest version.

If you are using an Application Server to run it, do check that your Application Server is using the latest version of JRE as well. Your environment variable may be pointing at the latest version of JRE, but your application server may not be doing the same.

Sign up to request clarification or add additional context in comments.

2 Comments

Would tomcat be my Application Server? Anyway how would check JRE version?
check startup.bat / startup.sh. you can see where tomcat uses

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.