Problem with starting OpenOffice service (soffice) from Java (command working in commandline, but not from Java)

南笙酒味 提交于 2019-12-04 15:46:40

I'm not sure if I'm not mistaken, but as far as I see you're generating the commands but never passing them to the "execute" method... you're executing "".

Try using Runtime.getRuntime().exec(commands) =)

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

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();
    }

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");
        }
    }

Note that this only works in Linux.

I believe you aren't handling quoting correctly. The original sh command line includes double quotes to prevent the shell interpreting the semicolons. The shell strips them off before the soffice process sees them.

In your Java code the shell will never see the arguments, so the extra double quotes (escaped with backslashes) are not needed - and they are probably confusing soffice.

Here's the code with the extra quotes stripped out (and a semicolon thrown in)

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");

(Disclaimer: I don't know Java, and I haven't tested this!)

"/Applications/OpenOffice.org\ 2.4.app/Contents/MacOS/soffice.bin -headless -nofirststartwizard -accept='socket,host=localhost,port=8100;urp;StartOffice.Service'"

or simply escaping the quotes will work as well. We feed a command like this to an ant script that ultimately ends up in an exec call like you have above. I would also recommend restarting the process every 500 or so conversions because OOO does not properly free memory (depending on what version you are running).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!