doushi3202 2017-04-13 02:05
浏览 77
已采纳

使用jasperreports Java桥和symfony时出现问题

I'm building a web application using symfony 2 php. And I want to generate some reports using jasperreports. I used Java Bridge and I have the Java bridge war deployed in my tomcat 7 but when I try to generate my reports I face an error.

So here is my code in my controller

 require_once("http://localhost:8080/JavaBridge/java/Java.inc");
 $reportsPath ="D:/jaspertest/";
        $reportFileName = "report";
        $jasperReportsLib = "D:/Programmes/jasperReport/lib";
        $java_library_path = '';
        $handle = @opendir($jasperReportsLib);
        while(($new_item = readdir($handle))!==false) {
            $java_library_path .= 'file:'.$jasperReportsLib.'/'.$new_item .';';
        }
        java_require($java_library_path);
       $sJcm = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
        $report = $sJcm->compileReport($reportsPath .$reportFileName.".jrxml");

    $map = new Java("java.util.HashMap");
    $emptyDataSource = new Java("net.sf.jasperreports.engine.JREmptyDataSource");
    $sJfm = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
    $print = $sJfm->fillReport($report,$map,$emptyDataSource);
    $sJem = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
    $sJem->exportReportToPdfFile($print, $reportsPath .$reportFileName.".pdf");
    set_time_limit(0);
    @readfile($reportsPath .$reportFileName.".pdf");// or die("problem occurs.");
    $response = new Response($reportsPath .$reportFileName.".pdf");
    $response->headers->set('Content-type', 'application/pdf');
    $response->headers->set('Content-Disposition', 'attachement; filename=Report.pdf');
    $response->headers->set('Cache-Control', 'must-revalidate');
    return $response; 

And here is the exception I get:

Symfony\Component\Debug\Exception\ContextErrorException: Notice: fwrite(): 
    at n/a
        in http://localhost:8080/JavaBridge/java/Java.inc line 482

    at Symfony\Component\Debug\ErrorHandler->handleError('8', 'fwrite(): ', 'http://localhost:8080/JavaBridge/java/Java.inc', '482', array('data' => ''))
        in  line 

    at fwrite(resource, '')
        in http://localhost:8080/JavaBridge/java/Java.inc line 482

    at java_SocketChannel->fwrite('')
        in http://localhost:8080/JavaBridge/java/Java.inc line 528

    at java_SocketHandler->write('')
        in http://localhost:8080/JavaBridge/java/Java.inc line 671

    at java_HttpHandler->redirect()
        in http://localhost:8080/JavaBridge/java/Java.inc line 743

    at java_Protocol->redirect()
        in http://localhost:8080/JavaBridge/java/Java.inc line 755

    at java_Protocol->finish()
        in http://localhost:8080/JavaBridge/java/Java.inc line 794

    at java_Protocol->invokeEnd()
        in http://localhost:8080/JavaBridge/java/Java.inc line 359

    at java_Client->invokeMethod('0', 'updateJarLibraryPath', array('file:D:/Programmes/jasperReport/lib/.;file:D:/Programmes/jasperReport/lib/..;file:D:/Programmes/jasperReport/lib/jasperreports-5.6.0.jar;file:D:/Programmes/jasperReport/lib/jasperreports-applet-5.6.0.jar;file:D:/Programmes/jasperReport/lib/jasperreports-fonts-5.6.0.jar;file:D:/Programmes/jasperReport/lib/jasperreports-javaflow-5.6.0.jar;', 'c:/wamp/bin/php/php5.5.12/ext/', 'C:\wamp\www\syslife_v1\web', '.;C:\php\pear'))
        in http://localhost:8080/JavaBridge/java/Java.inc line 986

    at java_require('file:D:/Programmes/jasperReport/lib/.;file:D:/Programmes/jasperReport/lib/..;file:D:/Programmes/jasperReport/lib/jasperreports-5.6.0.jar;file:D:/Programmes/jasperReport/lib/jasperreports-applet-5.6.0.jar;file:D:/Programmes/jasperReport/lib/jasperreports-fonts-5.6.0.jar;file:D:/Programmes/jasperReport/lib/jasperreports-javaflow-5.6.0.jar;')
        in C:\wamp\www\syslife_v1\src\MamdaIT\ComptabiliteBundle\Controller\BordereauController.php line 372

    at MamdaIT\ComptabiliteBundle\Controller\BordereauController->editionDetailBordereau(object(Request), '66')
        in  line 

    at call_user_func_array(array(object(BordereauController), 'editionDetailBordereau'), array(object(Request), '66'))
        in C:\wamp\www\syslife_v1\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php line 144

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in C:\wamp\www\syslife_v1\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php line 64

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in C:\wamp\www\syslife_v1\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel.php line 69

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in C:\wamp\www\syslife_v1\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php line 185

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in C:\wamp\www\syslife_v1\web\app_dev.php line 30

I looked everywhere but I couldn't find a solution, am I missing something?

展开全部

  • 写回答

1条回答 默认 最新

  • douju1968 2017-04-14 01:10
    关注

    Multiple problems here, but I'll try to explain step by step:

    1. The code part.

    You should not use java_require to dynamically add library dependencies from PHP (has been removed from v6).

    2. Error reporting

    As you noticed the symfony error handler couldn't provide very helpful information (no clear message). Not a symfony problem, but more about the way exceptions/errors are handled by the Java.inc client. The bridge exceptions (java bridge side) must be retrieved explicitly using getLastException (not 100% correct, but let's assume it to make things simple). Of course no error handler (symfony, whoops...) has support for this.


    So what to do ?

    1. Fix registration of your java libs

    Your dependencies must be either provided in the war bundle (what is often called the JavaBridgeTemplate.war) or in the tomcat global lib directory.

    Personnally I prefer the first method to prevent possible collisions and it can be achieved by either:

    • adding the jar's into your WEB-INF/lib directory inside the war file and redeploy (this is generally how it was done by most php devs).

    • Or another option (totally opinionated and based on a javabridge fork I initiated few months ago), is to build your own jar. As a quick example with jasper and mysql, you can download, unzip and build your own java bridge war with:

      wget https://github.com/belgattitude/php-java-bridge/archive/7.0.0.zip -O pjb.zip
      unzip pjb.zip && cd php-java-bridge-7.0.0
      ./gradlew war -I init-scripts/init.jasperreports.gradle -I init-scripts/init.mysql.gradle
      

    The builded war file will be available in the ./build/libs folder, ready to deploy.

    A more complete installation is located: http://docs.soluble.io/soluble-japha/install_server/#build-and-customize

    2. The error reporting problem.

    The symfony error handler (or whoops...) will not play well with the Java.inc client and debugging can really be a mess. I suggest to use the soluble-japha client instead: https://github.com/belgattitude/soluble-japha which, amongst other things, will throw exceptions in a more standard way, see here.

    The soluble-japha is just the client part, it replaces the Java.inc and is compatible with the official php-java-bridge 6/7 and the soluble php-java-bridge server implementations. You'll have to slighty change you PHP code but nothing very hard. See the documentation website.

    From there you might have a better view of the problem.

    Hope it helps.

    展开全部

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

报告相同问题?

悬赏问题

  • ¥100 二维码被拦截如何处理
  • ¥15 怎么解决LogIn.vue中多出来的div
  • ¥15 优博讯dt50巴枪怎么提取镜像
  • ¥30 在CodBlock上用c++语言运行
  • ¥15 求C6748 IIC EEPROM程序固化烧写算法
  • ¥50 关于#php#的问题,请各位专家解答!
  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
手机看
程序员都在用的中文IT技术交流社区

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

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

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

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

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

客服 返回
顶部