0

I try to load the "Tasks" from a .mpp-File. If I get the current date by java.util.Date it works very fine, to access JAVA but if i try to access the MPPReader I get the error:

CreateInstance failed: new net.sf.mpxj.mpp.MPPReader. Cause: java.lang.ClassNotFoundException 

This is the code to my test case.

define("JAVA_SERVLET", false);
define("JAVA_HOSTS", 9267);

$mpxjPath = getcwd()."/libs/java/mpxj-5.6.0.jar";
exec("java -cp $mpxjPath");
require_once("libs/java/Java.inc");

$date = new java('java.util.Date');
echo $date;

$project = new java('net.sf.mpxj.mpp.MPPReader');

This is the output in the browser

Wed Feb 01 08:53:43 CET 2017
Fatal error: Uncaught [[o:Exception]:"java.lang.Exception: CreateInstance failed: new net.sf.mpxj.mpp.MPPReader. Cause: java.lang.ClassNotFoundException: net.sf.mpxj.mpp.MPPReader VM: 1.7.0_55@http://java.oracle.com/" at:
    #-15 java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    #-14 java.net.URLClassLoader$1.run(URLClassLoader.java:355)
 ...

The output shows, that the java is reachable. But how can I access the MPPReader ?


Update

Server: CentOS 7

This is the code in my index.php right now.

<?php
define("JAVA_HOSTS", 9267);
define("JAVA_SERVLET", false);

require_once("libs/java/Java.inc");

echo "<pre>";
$date = new java('java.util.Date');
echo $date;

$project = new java('net.sf.mpxj.mpp.MPPReader');

?>

Run Standalone command on CentOS 7

java -cp "ext/JavaBridge.jar:libs/java/mpxj-5.6.0.jar" php.java.bridge.Standalone SERVLET:9267

This is the error.

Thu Feb 02 07:11:45 CET 2017PHP Fatal error:  Uncaught [[o:Exception]:"java.lang.Exception: CreateInstance failed: new java.net.sf.mpxj.mpp.MPPReader. Cause: java.lang.ClassNotFoundException: java.net.sf.mpxj.mpp.MPPReader VM: 1.7.0_55@http://java.oracle.com/" at:
#-15 java.net.URLClassLoader$1.run(URLClassLoader.java:366)
#-14 java.net.URLClassLoader$1.run(URLClassLoader.java:355)
#-13 java.security.AccessController.doPrivileged(Native Method)
#-12 java.net.URLClassLoader.findClass(URLClassLoader.java:354)
#-11 java.lang.ClassLoader.loadClass(ClassLoader.java:425)
#-10 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
#-9 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
#-8 java.lang.Class.forName0(Native Method)
#-7 java.lang.Class.forName(Class.java:270)
#-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.ContextRunner.run(Co in http://127.0.0.1:9267/JavaBridge/java/Java.inc on line 195

1 Answer 1

0

Looking at your attempt to load the mpxj-5.6.0.jar, I noticed you tried to make it available to the JVM from the client side (PHP):

$mpxjPath = getcwd()."/libs/java/mpxj-5.6.0.jar";
exec("java -cp $mpxjPath");
require_once("libs/java/Java.inc");

The idea looks appealing, but sadly cannot work that way.


Remember the java-bridge is made of two parts, the client (Java.inc / PHP) and server (PHPJavaBridge / Generally a servlet). So if you want to add a library to the classpath, it must be done on the server side (available to the JVM).

From your code example (define("JAVA_SERVLET", false)), I assume you're not starting the bridge through Tomcat/servlet env, but with the standalone server. In this scenario, you'll have to add the *.jar on the classpath when starting the server.

You'll end up doing something approx like this to start the server :

$ java -cp "./JavaBridge.jar:/path/to/mpxj-5.6.0.jar" php.java.bridge.Standalone SERVLET:9267

edit: replace /path/to/ in by the correct location of the jar.


If you're working on Linux/Mac, have a look at the pjbserver-tools project. It provides a php-wrapper around the standalone server where you can easily configure deps.

Just keep aware of two things:

  • Servlet mode (.war/tomcat) is the way to go if you intend to use it in production. If you're unsure about how it works, see the pjb-starter-gradle which contains documentation.
  • The older Java.inc won't run on PHP7 and support won't probably be continued... At least take time to consider using the reworked client: soluble-japha. It might even be easier to work with it, but of course, as the author, I'm biased. Check for yourself ;)

Note for servlet tomcat:

For servlet tomcat install, same idea applies. Just make sure your .jar file(s) are present in WEB-INF\libdirectory. See also the unoffical php-java-bridge fork to get the latest .war template.

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

5 Comments

well, add the .jar-File to the classpath in the server. but If i run the index.php page i recieve the same error An exception occured: java.lang.ClassNotFoundException: net.sf.mpxj.mpp.MPPReader.
So it's not yet made ;) Can you update your problem description and include how you start the server (command line used if applicable), platform (windows/unix) and clarify the mode Tomcat/Servlet or StandAlone ? This way, I might be more precise in answer... too much guess right now ;) (could be nice also to be sure what you call index.php, Is it your script example ? just to be sure)
Just made a small edit to include the path for the jar in the command. Can you try ?
Well.. is fixed! :) The classpath was shown on the java direct and not on the folder!! now it works! Thank you for your support!
Great! if you use composer really consider the updated client github.com/belgattitude/soluble-japha. Compatible with the phpjavabridge server, a little more verbose but you'll avoid a lot of issues later. Just my 2 cents ;)

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.