您好我想使用java
从命令提示符运行一些东西我想转到以下目录C:\Program Files\OpenOffice.org 3\program\
然后跑
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
我试过但我无法做到!
我的代码
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Runtime rt = Runtime.getRuntime();
//Process pr = rt.exec("cmd /c dir");
// Process pr = rt.exec("cmd /c dir");
Process pr = rt.exec(new String[]{"C:\\Program Files\\OpenOffice.org 3\\program\\soffice",
"-headless",
"-accept='socket,host=127.0.0.1,port=8100;urp;'",
"-nofirststartwizard"});
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
答案 0 :(得分:6)
不要使用cd
,并使用字符串数组方法:
rt.exec(new String[]{"C:\\Program Files\\OpenOffice.org 3\\program\\soffice.exe",
"-headless",
"-accept='socket,host=127.0.0.1,port=8100;urp;'",
"-nofirststartwizard"});
答案 1 :(得分:3)
最后我解决了它
String[] SOFFICE_CMD = { "C:/Program Files/OpenOffice.org 3/program/soffice", "-accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager", "-invisible", "-nologo"};
Runtime.getRuntime().exec(SOFFICE_CMD);
感谢大家的支持!!
答案 2 :(得分:2)
首先尝试直接使用所有属性等从命令提示符运行您想要运行的任何内容。一旦您从命令提示符处成功运行服务/应用程序,请执行2。
将命令保存在.bat文件中。
例如:C:\ m-admin \ app.exe我将其保存为C:\
上的app.bat例如:
ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd", "/c","C:\\app.bat"});
Process pr = builder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
答案 3 :(得分:1)
我已使用流程构建器方法编辑了代码(如下)。看看这是否适合你。由于访问冲突,使用exec有时不起作用:
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Runtime rt = Runtime.getRuntime();
//Process pr = rt.exec("cmd /c dir");
// Process pr = rt.exec("cmd /c dir");
ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd", "/c", "C:\\Program Files\\OpenOffice.org 3\\program", "soffice",
"-headless",
"-accept='socket,host=127.0.0.1,port=8100;urp;'",
"-nofirststartwizard"});
Process pr = builder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}
答案 4 :(得分:1)
我认为我发现了你的错误:将你的论点改为以下内容:看看它是否有效:
(new String[]{"cmd", "/c", "C:\\Program Files\\OpenOffice.org 3\\program\\soffice",
"-headless",
"-accept='socket,host=127.0.0.1,port=8100;urp;'",
"-nofirststartwizard"})
答案 5 :(得分:0)