简体   繁体   English

从Java代码运行exe

[英]Running exe from java code

I've been working on in a swing application. 我一直在一个秋千应用程序中工作。 I've a JFrame having some buttons and fields. 我有一个带有一些按钮和字段的JFrame。

On some button click event,I'm opening an exe from my current directory. 在某些按钮单击事件中,我正在从当前目录中打开一个exe。 Everything works fine. 一切正常。

try {
      Runtime.getRuntime().exec(System.getProperty("user.dir") +
      "\\Upgrade\\Upgrade.exe");
      } catch (IOException ex) {
       ex.printStacktrace();
        }
   this.dispose(); // disposing my current java file.

But what i need is to exit the java code after opening the exe file. 但是我需要打开exe文件后退出 Java代码。

Anyone help to deal this.? 有人帮忙解决这个问题吗?

Can you try making it a process, then waiting for that process to finish before exiting, like so: 您是否可以尝试使其成为一个过程,然后等待该过程完成再退出,就像这样:

class SO {
public static void main(String args[]) {
try {
    Process proc = Runtime.getRuntime().exec("your command");
    proc.waitFor(); //Wait for it to finish
    System.exit(0);
} catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}
}
}

Executing an application from java using Runtime.exec() is a source of well known problems. 使用Runtime.exec()从Java执行应用程序是众所周知的问题的来源。 Like waiting for a Process to finish, but not consuming the data from the stream buffers is a sure way for you application to hang. 就像等待进程完成一样,但是不使用流缓冲区中的数据,这是挂起应用程序的肯定方法。

I would suggest you use a library like Apache Common Exec to handle this. 我建议您使用Apache Common Exec之类的库来处理此问题。

I worked on a project a while back where we used Runtime.exec() to launch a process that would eventually extract files over existing files. 不久前,我在一个项目上工作,我们使用Runtime.exec()启动了一个过程,该过程最终将在现有文件上提取文件。 All worked well except on one machine used for staging - it would just hang. 除了在一台用于登台的机器上,其他一切都运行良好-它会挂起。 It turned out on that staging machine, someone had set the date/time back so it looked liked the new files being extracted where older than the existing one, causing the external process to generate a warning message for each, overflowing the error buffer - which our application was not consuming! 原来在那台登台计算机上,有人将日期/时间重新设置了,所以看起来好像要提取比现有文件更旧的新文件,从而导致外部进程为每个文件生成警告消息,从而使错误缓冲区溢出-我们的应用程序没有消耗!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM