[英]Run command prompt from java?
嗨,我想使用 java 从命令提示符运行一些东西
我想 go 到以下目录C:\Program Files\OpenOffice.org 3\program\
然后运行soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
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();
}
}
不要使用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"});
最后我解决了
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);
谢谢大家的支持!!
@Harinder:我想建议一种替代方法。 你能做的是;
首先尝试使用所有属性等直接从命令提示符运行您想要运行的任何内容。一旦您从命令提示符成功运行服务/应用程序,直接执行 2。
Go 并将命令保存在 .bat 文件中。
例如:C:\m-admin\app.exe 我在 C:\
例如:
ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd", "/c","C:\\app.bat"});
Process pr = builder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
我已经使用流程构建器方法编辑了代码(如下)。 看看这是否适合你。 由于访问冲突,使用 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();
}
}
}
我想我发现了你的错误:将你的论点更改为以下内容:看看它是否有效:
(new String[]{"cmd", "/c", "C:\\Program Files\\OpenOffice.org 3\\program\\soffice",
"-headless",
"-accept='socket,host=127.0.0.1,port=8100;urp;'",
"-nofirststartwizard"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.