gpt4 book ai didi

java - 从java运行命令提示符?

转载 作者:搜寻专家 更新时间:2023-11-01 01:14:34 26 4
gpt4 key购买 nike

您好,我想使用 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();
}
}

最佳答案

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

关于java - 从java运行命令提示符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6274448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com