停止可执行 jar 文件

发布于 2024-08-01 17:43:46 字数 92 浏览 2 评论 0原文

我有一个可执行的 jar 文件。 一旦启动,停止它的唯一方法是转到任务管理器并结束 javaw 进程。 有没有更干净的方法来阻止它,比如使用新手用户可以使用的 UI?

I have an executable jar file. Once started the only way to stop it is to go to the task manager and end the javaw process. Is there a cleaner way to stop it, say with an UI which a novice user can use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

☆獨立☆ 2024-08-08 17:43:47

由于您控制代码,因此您希望 GUI 中有一些允许使用 System.exit 退出的内容。 因此,如果您使用 Swing,您可以执行以下操作:

JButton b = new JButton("Exit");
b.setActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e) {
    System.exit(0);
  }
}

Since you control the code, you want to have something in the GUI that will allow for exiting using System.exit. So, if you were using Swing, you could do something like:

JButton b = new JButton("Exit");
b.setActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e) {
    System.exit(0);
  }
}
伤痕我心 2024-08-08 17:43:47

我终于找到了解决我的问题的方法。 因为我没有可以关闭的 GUI,也没有控制台。 最好的方法是使用 java.awt 包中的 SystemTray API。 只需右键单击该图标并退出即可。

I finally found the solution to my problem. Since I don't have a GUI which I can close, nor do I have a console. The best way is to use the SystemTray API in the java.awt package. Just right click on the icon and exit.

森林迷了鹿 2024-08-08 17:43:47

如果您的应用程序没有 GUI(例如,如果它是服务),那么您可以使用本地网络访问来模拟操作系统通常用于启动和停止服务的标准 IPC(进程间通信)机制。

您可以在 Apache 的 Commons Daemon 项目中找到一个非常流行的实现:http://commons.apache.org/守护进程/

If your application does not have a GUI - for example if its a service - then you can use local network access to simulate the standard IPC (InterProcess Communication) mechanisms that the operating system normally uses to start and stop services.

A very popular implementation of that you can find in Apache's Commons Daemon project: http://commons.apache.org/daemon/

花间憩 2024-08-08 17:43:47

当所有非守护线程结束时,您的应用程序将结束。 实现此目的的一种方法是让所有线程只保留一个守护进程,并让最后一个线程等待信号停止。

Your application will end when all non-daemon threads have ended. A way to do this, would be to make all threads but one daemons, and let the last thread be the one that awaits the signal to stop.

寻梦旅人 2024-08-08 17:43:47

另一种有用的技术是监听 windowClosing,当用户单击 Windows 上的 X 按钮(以及其他系统上的等效按钮)时,就会发生这种情况:

addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
});

我通常将其放在为应用程序扩展 Frame 的类的构造函数中。

如果您使用 JFrame,还需要添加以下行:

setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

Another useful technique is to listen for windowClosing, which happens when the user clicks the X button on Windows (and the equivalent on other systems):

addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
});

I usually put that in the constructor of the class that extends Frame for the application.

If you are using a JFrame, you also need to add this line:

setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文