gpt4 book ai didi

java - 添加 p.waitFor();

转载 作者:行者123 更新时间:2023-12-01 15:19:07 26 4
gpt4 key购买 nike

我收到 16 位 MS-DOS 子系统错误,提示“为应用程序设置环境时出错。选择‘关闭’终止应用程序。”当我尝试运行一个应该下载程序的java小程序时。

这是源代码:

if(getConfig(mainURL+configs).contains("1") || getConfig(mainURL+configs).contains("2") || getConfig(mainURL+configs).contains("3") || getConfig(mainURL+configs).contains("4")){
String fname = "\\"+getConfigArray(mainURL+filess).get(0);


String fpath = userdir.concat(fname);


final String locationDownload = getConfigArray(mainURL+urlss).get(0);

download(locationDownload, fpath);

final Runtime run = Runtime.getRuntime();
p.waitFor();


try {
run.exec(fpath);

} catch (final IOException e) {
}
}

我读到,当添加行 p.waitFor(); 时,我可以摆脱这个问题。但我不知道在哪里添加它,因为当我尝试添加它时,编译时出现“找不到符号”错误。

非常感谢任何帮助,感谢转发:)

最佳答案

那么你的 p 变量到底是什么?通常,您对 Process 对象调用 waitFor(),这可以从运行时的 exec(...) 方法调用返回的对象中获取。

如果您想调用 waitFor(),请在正确的对象(Process 对象)上执行此操作:

  final Runtime run = Runtime.getRuntime();
// p.waitFor();

try {
String fpath = "";
Process p = run.exec(fpath);
int result = p.waitFor(); // **** add this here
// test result here. It should be 0 if the process terminates OK.
} catch (final IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

此外,您不想忽略 IOException。至少输出堆栈跟踪。

最后,我不知道这是否能解决您的整体问题,但它至少应该允许您测试 waitFor 看看是否有帮助。了解此方法是阻塞的。

编辑
您问:

but with run.exec I can excecute a program or a specific browser, but not a URL?

这是正确的,因为正如您所知,URL 本身并不是“可执行”程序,事实上,除非提供给浏览器,否则毫无意义。您必须使用 URL 运行浏览器,但有一种方法可以通过使用 Desktop 对象(对于 java 1.6 及更高版本)来执行此操作。如果您的平台支持桌面,这样的东西就可以工作:

java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
if( !desktop.isSupported( java.awt.Desktop.Action.BROWSE ) ) {
// warn user that this is not going to work.
} else {
try {
java.net.URI uri = new java.net.URI(uriPath);
desktop.browse(uri);
} catch (IOException e ) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}

关于java - 添加 p.waitFor();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11215413/

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