如何让您的 Java 应用程序自行重启

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20169108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:41:03  来源:igfitidea点击:

How to make your java application restarts itself

javaapplication-restart

提问by Yoda

I want to implement resetfeature in my application which cleans up some directories, copies files etc. then in order to complete the process I need to restart it.

我想reset在我的应用程序中实现清理一些目录、复制文件等的功能,然后为了完成我需要重新启动它的过程。

How to make application reruns itself? I think opening second instance and closing this one would be enough, altough it is not real restart.

如何使应用程序重新运行?我认为打开第二个实例并关闭这个就足够了,尽管它不是真正的重启。

My application's core is class extending JFramebut there is lot of static blocks which read class's extensions when the program is executed. I need to restart programatically my application so all of the static collection and blocks will be created/executed again.

我的应用程序的核心是类扩展,JFrame但有很多静态块在程序执行时读取类的扩展。我需要以编程方式重新启动我的应用程序,以便再次创建/执行所有静态集合和块。

It starts that way.

它是这样开始的。

SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Window().createGUI();
        }
    });

This seems work fine:

这似乎工作正常:

public void restart() {
    /*  dispose();
    Window.main(null);*/
    StringBuilder cmd = new StringBuilder();
    cmd.append(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java ");
    for (String jvmArg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
        cmd.append(jvmArg + " ");
    }
    cmd.append("-cp ").append(ManagementFactory.getRuntimeMXBean().getClassPath()).append(" ");
    cmd.append(Window.class.getName()).append(" ");

    try {
        Runtime.getRuntime().exec(cmd.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.exit(0);
}

采纳答案by Yoda

This is from the other topic but in the opposite to the accepted question in this other topic this one really works.

这是来自另一个主题,但与另一个主题中已接受的问题相反,这个问题确实有效。

public void restart() {
          StringBuilder cmd = new StringBuilder();
            cmd.append(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java ");
            for (String jvmArg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
                cmd.append(jvmArg + " ");
            }
            cmd.append("-cp ").append(ManagementFactory.getRuntimeMXBean().getClassPath()).append(" ");
            cmd.append(Window.class.getName()).append(" ");

            try {
                Runtime.getRuntime().exec(cmd.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.exit(0);
    }


27/02/2018:I believe that Mark posted better solution: https://stackoverflow.com/a/48992863/1123020

27/02/2018:我相信 Mark 发布了更好的解决方案:https: //stackoverflow.com/a/48992863/1123020

回答by Hovercraft Full Of Eels

Your question / my comments:

您的问题/我的评论:

I want to implement reset feature in my application which cleans up some directories, copies files etc. then in order to complete the process I need to restart it. How to make application reruns itself?

我想在我的应用程序中实现重置功能,它会清理一些目录、复制文件等,然后为了完成我需要重新启动它的过程。如何使应用程序重新运行?

This is a common feature/need of many applications, and not just academic assignments. Unfortunately there is no one-size-fits all solution to this, and it will all depend on the specifics of your program. If your program is very modular and written with smart M-V-C separation of concerns, it becomes much easier to do this, often just resetting the model to its initial state, or by loading a new model into the GUI.

这是许多应用程序的共同特征/需求,而不仅仅是学术作业。不幸的是,没有一刀切的解决方案,这完全取决于您的程序的具体情况。如果您的程序非常模块化并且使用智能 MVC 关注点分离编写,那么执行此操作会变得更加容易,通常只需将模型重置为其初始状态,或者通过将新模型加载到 GUI 中。

I think opening second instance and closing this one would be enough, altough it is not real restart.

我认为打开第二个实例并关闭这个就足够了,尽管它不是真正的重启。

I think that this is a very bad idea. Better to simply reset the state of your text components, buttons, checkboxes, etc to their original state. Again, the more modular your code, the easier it is to do. Each separate module could have its own reset()method that takes care of initializing it.

我认为这是一个非常糟糕的主意。最好简单地将文本组件、按钮、复选框等的状态重置为其原始状态。同样,您的代码越模块化,就越容易做到。每个单独的模块都可以有自己的reset()方法来负责初始化它。

In my case I want to reload the JFrame consisting of many JPanels. I have done that:

就我而言,我想重新加载由许多 JPanel 组成的 JFrame。我已经这样做了:

Again, I urge you not to go this route.

我再次敦促你不要走这条路。

You could place some of your JTextComponents into an ArrayList for ease of resetting. For example, you could reset your GUI fields inside of reset. Something like:

您可以将一些 JTextComponents 放入 ArrayList 以便于重置。例如,您可以在 reset 内重置 GUI 字段。就像是:

public void reset() {
  // assuming you have an ArrayList of JTextComponents called textComponents
  for (JTextComponent textComponent : textComponents) {
    textComponent.setText("");
  }

  // same if you had a bunch of comboboxes in a List called comboBoxes
  for (JComboBox comboBox : comboBoxes) {
    comboBox.setSelection(-1); // consider removing listeners first, then re-adding them

  //   etc for other components
  }
}


Edit

编辑

I found out that my solution -> disposing JFrame is not so good. I have a lot of static blocks, loading serialized files etc. I need to really restart it. Maybe there is easy way to application execute itself.

我发现我的解决方案 -> 处理 JFrame 不太好。我有很多静态块,加载序列化文件等。我需要真正重新启动它。也许有简单的方法可以让应用程序自行执行。

Sorry, but this only suggests to me that your program could probably have its organization improved upon. It should be reset-able, and if not, consider changing it so that it can be so. Does it follow a Model-View-Control structure? If not, consider doing this.

抱歉,但这只是向我表明您的程序可能会改进其组织。它应该是可重置的,如果不是,请考虑更改它以使其可以重置。它是否遵循模型-视图-控制结构?如果没有,请考虑这样做。