openoffice4.1.5 Linux环境无法通过代码启动

springboot通过Java代码Runtime.getRuntime().exec(OpenOffice_Home+soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard ),在Windows环境能正常启动openoffice服务器,但是部署到Linux服务器却跑不起来。可以使用.sh脚本完美解决

#! /bin/sh
cd /opt/openoffice4/program
./soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

从官网下载openoffice的Linux安装包

http://www.openoffice.org/download/index.html

默认会安装到 /opt/openoffice4

详解整个流程

//局部代码,非完整代码
private String OpenOffice_HOME ="/opt/openoffice4";

String command = OpenOffice_HOME + "/program/soffice -headless -accept=\"socket,host=127.0.0.1,port=" + i + ";urp;\" -nofirststartwizard &";


process = Runtime.getRuntime().exec(command);

我想出现这个问题就是上述代码通过.exec控制openoffice服务的开关,出现这个问题的原因可能在于java通过Runtime.getRuntime().exec(),这个命令根本无法通过代码控制openoffice的开启和关闭。那么这个时候就需要转换一下思路,我把控制openoffice启动的命令放到.sh脚本里,然后通过Runtime.getRuntime().exec()启动这个脚本不就行了?

//xxxx可以是你的项目名,非完整代码
String command = "/root/XXXX/runOpenOffice.sh";//采用脚本的方式开启openoffice

process = Runtime.getRuntime().exec(command);

runOpenOffice.sh

#! /bin/sh
cd /opt/openoffice4/program
./soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

stopOpenOffice.sh

#! /bin/sh
ps -ef |grep soffice |grep -vE 'grep|soffice\.bin'|awk '{print $2}'| xargs kill

通过这种方式,解决了我的问题。开关openoffice道理一样,都是通过Runtime.getRuntime().exec()调用Linux服务器下 /root/runOpenOffice.sh和/root/stopOpenOffice.sh。

posted @ 2018-09-05 16:35  TroubleBoy丶  阅读(312)  评论(0编辑  收藏  举报