0

我收到一个 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(); 行时摆脱它。但我不知道在哪里添加它,因为当我尝试添加它时,编译时出现“找不到符号”错误。

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

4

1 回答 1

1

那么你的 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();
  }

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

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

编辑
你问:

但是使用 run.exec 我可以执行程序或特定浏览器,但不能执行 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();
   }
}
于 2012-06-26T21:27:03.570 回答