问题
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;"
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");
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?
Why doesn't this work?
回答1:
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) =)
回答2:
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.
回答3:
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!)
回答4:
"/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).
来源:https://stackoverflow.com/questions/378338/problem-with-starting-openoffice-service-soffice-from-java-command-working-in