doumeng3188 2017-02-01 08:02
浏览 161
已采纳

在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");
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条回答 默认 最新

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动