2017-02-01 08:02
浏览 151

在PHPJAVA Bridge中包含.jar-File

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");

$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@" at:

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


Server: CentOS 7

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

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


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" 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 Cause: java.lang.ClassNotFoundException: VM: 1.7.0_55@" at:
#-13 Method)
#-11 java.lang.ClassLoader.loadClass(
#-10 sun.misc.Launcher$AppClassLoader.loadClass(
#-9 java.lang.ClassLoader.loadClass(
#-8 java.lang.Class.forName0(Native Method)
#-7 java.lang.Class.forName(
#-2 in on line 195
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanpo7354 2017-02-01 11:10

    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");

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

    Remember the java-bridge is made of two parts, the client ( / 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" 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 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.

    解决 无用
    打赏 举报

相关推荐 更多相似问题