从Java启动OpenOffice服务(soffice)的问题(命令在命令行工作,但不是从Java) [英] Problem with starting OpenOffice service (soffice) from Java (command working in commandline, but not from Java)

查看:1551
本文介绍了从Java启动OpenOffice服务(soffice)的问题(命令在命令行工作,但不是从Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个简单的命令,它从shell工作,但不能从Java工作。
这是我想要执行的命令,它工作正常:

I want to exceute a simple command which works from the shell but doesn't work from Java. This is the command I want to execute, which works fine:

soffice -headless "-accept=socket,host=localhost,port=8100;urp;" 

这是我从Java试图运行此命令的代码:

This is the code I am excecuting from Java trying to run this command:

String[] commands = new String[] {"soffice","-headless","\"-accept=socket,host=localhost,port=8100;urp;\""};
Process process = Runtime.getRuntime().exec(commands)
int code = process.waitFor();
if(code == 0)
    System.out.println("Commands executed successfully");

当我运行这个程序时,我得到命令执行成功。
然而,当程序完成时,进程不运行。
JVM是否可以在程序运行后杀死程序?

When I run this program I get "Commands executed successfully". However the process is not running when the program finishes. Is it possible that the JVM kills the program after it has run?

为什么这不工作?

推荐答案

我想说的是如何解决这个问题。
我创建了一个sh脚本,基本上为我运行soffice的命令。

I would like to say how I solved this. I created a sh script that basically run the command of soffice for me.

然后从Java我只是运行脚本,它工作正常,像这样:

Then from Java I just run the script, and it works fine, like this:


public void startSOfficeService() throws InterruptedException, IOException {
        //First we need to check if the soffice process is running
        String commands = "pgrep soffice";
        Process process = Runtime.getRuntime().exec(commands);
        //Need to wait for this command to execute
        int code = process.waitFor();

        //If we get anything back from readLine, then we know the process is running
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        if (in.readLine() == null) {
            //Nothing back, then we should execute the process
            process = Runtime.getRuntime().exec("/etc/init.d/soffice.sh");
            code = process.waitFor();
            log.debug("soffice script started");
        } else {
            log.debug("soffice script is already running");
        }

        in.close();
    }

我也通过调用这个方法来杀死soffice进程:

I also kill the soffice process by calling this method:


public void killSOfficeProcess() throws IOException {
        if (System.getProperty("os.name").matches(("(?i).*Linux.*"))) {
            Runtime.getRuntime().exec("pkill soffice");
        }
    }

请注意,这只适用于Linux。

Note that this only works in Linux.

这篇关于从Java启动OpenOffice服务(soffice)的问题(命令在命令行工作,但不是从Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆