我想执行一个简单的命令,该命令可以在 shell 中运行,但不能在 Java 中运行。 这是我要执行的命令,效果很好:
soffice -headless "-accept=socket,host=localhost,port=8100;urp;"
这是我从 Java 尝试运行此命令时执行的代码:
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在程序运行后将其杀死?
为什么这不起作用?
最佳答案
我不确定我是否弄错了,但据我所知,您正在生成命令,但从未将它们传递给“execute”方法...您正在执行“”。
尝试使用 Runtime.getRuntime().exec(commands) =)
关于java - 从 Java 启动 OpenOffice 服务 (soffice) 时出现问题(命令在命令行中运行,但不能从 Java 运行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/378338/