linux tomcat部署含有matlab画图打包的java web程序

首先说下问题:matlab可以把相关算法代码打包成jar文件共java调用,本例使用的jar文件的作用是画图并保存,然后部署在linux的tomcat中进行发布。这里出现了一个问题,具体如下:linux机器是在我的虚拟机上面(可以作为一个远程服务器A),所以我使用xmanager连接A机器,然后启动tomcat进行web程序的部署,然后使用浏览器访问发布的web程序,然后运行测试ok。但是,当我发布tomcat程序后把xmanager关闭后,再次访问web程序,出现错误,保存不了图片。(另外,如果我是在A服务器上面打开terminal进行tomcat web程序的发布,然后关闭terminal,这样的操作是没有问题的,web测试一样ok)

后台报错如下:

unable to open display 'localhost:10.0' You will not be able to display graphics on the screen

其中一个相关的mathwork帖子如下: http://www.mathworks.com/matlabcentral/newsreader/view_thread/308200,不过好像没有答案;

解决方案1:使用远程桌面,配置xmanager的远程桌面到A服务器(具体可以参考:http://www.hack520.com/linux-xmanager-xbrowser/ , 没有亲自试过,好像很麻烦的样子),然后打开一个终端,进行tomcat的web发布,然后关闭xmanager。看web测试是否ok,如果ok,说明此种方案是ok。(这种方案lz还没有测试过);

解决方案2:使用screen。看问题,感觉是关闭了一些不该关闭的东西,使用screen可以后台运行tomcat,这个应该是和服务器A里面发布tomcat一样的,所以这种方案可实现的可能性应该比较大,关于screen的参考:http://hi.baidu.com/widebright/item/e17061c3c44660bb0c0a7b6a ,这个screen安装也有点麻烦(yum使用有问题,所以这种方案lz也没有测试过);

解决方案3:从源头出发,在使用matlab打包的时候,首先进行java文件对jar中的算法进行测试,然后才使用web的jsp页面调用的;但是在java调用中会出现下面的警告:

Warning: 2 invisible figure(s) exist at MCR termination.

If your application has terminated unexpectedly, please note that 
 applications generated by the MATLAB Compiler terminate when there are no 
 visible figure windows. See the documentation for WaitForFiguresToDie and
 WAITFORCALLBACKS for more information.

Warning: Class
    'graph2d.lineseries'
in use at MCR termination.

If your application has terminated unexpectedly, please note that 
 applications generated by the MATLAB Compiler terminate when there are no 
 visible figure windows. See the documentation for WaitForFiguresToDie and
 WAITFORCALLBACKS for more information.

Warning: Class
    'scribe.legend'
in use at MCR termination.
。。。

这个的大概意思是说当没有可视化的图像是,由matlab compiler编译的程序会意外终止,所以lz就上网找,找到了一篇关于c++的,如下: http://blog.csdn.net/owldestiny/article/details/4210177,我仿造他的方式(我的是java),修改了matlab的源码在plot函数前面加入了waitforcallbacks(true)然后在plot函数后面加上了pause(5);close(f)waitforcallbacks(false);(这里设置visible为true,之前都是设置visible为false的,这样可以避免弹出画图的窗口,而直接保存)意思和c++的那篇差不多,然后使用java 文件进行测试ok,没有出现警告信息。但是当我使用web发布的时候关闭xmanager还是不行。所以这种方案应该不适合。(lz亲测)

解决方案4:偶然的灵感,我在想可否使用xmanager首先登陆到虚拟机B然后再由虚拟机B登陆到虚拟机A上面,然后发布程序,然后关闭xmanager,这样是否可以行呢?经过lz的测试,发现这种方案是可行的,可以访问且能保存图像。


说明:如果要在linux上面发布tomcat程序(含有matlab打包的jar文件的时候),需要首先配置环境,即matlab的环境,具体可以参考lz之前的相关blog;

分享,快乐,成长


转载请注明出处:http://blog.csdn.net/fansy1990



原文地址:https://www.cnblogs.com/riskyer/p/3297125.html