doumeng3188 2017-02-01 00: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 03: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 sql server2008r2索引超出了数组界限
  • ¥15 MATLAB图片转灰度格式问题
  • ¥15 把h5作品链接复制到自己的账号里
  • ¥15 ensp抓包实验配置
  • ¥15 强化学习算法、MRO
  • ¥15 想要学习一门技术来保证以后能够吃上碗饭,该学什么好
  • ¥50 Verilog硬件开发,射频分析求解答
  • ¥20 matlab绘冲床平面连杆机构图和仿真,求帮助
  • ¥15 为什么树莓派5b显示禁止连接
  • ¥15 请专家处理报错!基于深度学习的车型分类问题:数据集为包含 10 种车型的图像数据集,分为训练集、验证集和测试集。
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部