Windows关闭

时间:2016-01-25 13:46:39

标签: java swing eclipse-rcp shutdown-hook

在使用Java 7运行的Eclipse E4 RCP应用程序中,我遇到的问题是,当Windows(7)关闭或注销时,不会执行shutdown-hook。

经过一些研究后,我发现了以下Open JDK bug entry,它创建了一个空的Swing JFrame并使用了一个shutdown-hook。关闭钩子创建一个文件并写入它。虽然这个bug与我的问题没有直接关系,但我接受了代码并对其进行了一些修改(见下文)。 (最大的修改是它不会陷入困境。)

之后我从该代码创建了一个jar并用javaw -jar jarname.jar执行它。然后我尝试注销并关闭,在这两种情况下都写入了预期的文件。

之后我修改了代码,以便创建一个空的SWT Shell并使用相同的shutdown-hook(另见下文)。注销和关闭时,不会写入预期的文件。

现在我想知道为什么行为如此不同?我发现特别难以找到有关Windows和关机钩的最新信息。

此外,如何确保在我的RCP应用程序中执行shutdown-hook?

Swing code

public class ShutdownHookSwingBug {

  public static final String fileName = "C:/shutdownhookbug_swing.log";

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    File myFile = new File(fileName);
    myFile.delete();

    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        try {
          // Write "Hello " to a file
          File myFile = new File(fileName);
          FileOutputStream outputStream = new FileOutputStream(myFile);
          outputStream.write("Hello ".getBytes());
          outputStream.flush();
          // Write "world!" and close the file
          outputStream.write("world!".getBytes());
          outputStream.flush();
          outputStream.close();
        } catch (Exception e) {
          // Shouldn't happen.
        }
      }
    });
  }
}

SWT代码

public class ShutdownHookSwtBug {

  public static final String fileName = "C:/shutdownhookbug_swt.log";

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(300, 300);

    File myFile = new File(fileName);
    myFile.delete();

    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        try {
          File myFile = new File(fileName);
          FileOutputStream outputStream = new FileOutputStream(myFile);
          outputStream.write("Hello ".getBytes());
          outputStream.flush();
          outputStream.write("world!".getBytes());
          outputStream.flush();
          outputStream.close();
        } catch (Exception e) {
          // Shouldn't happen.
        }
      }
    });

    shell.open();
    while(!shell.isDisposed()){
      if(!display.readAndDispatch()){
        display.sleep();
      }
    }
    display.dispose();
  }
}

1 个答案:

答案 0 :(得分:2)

作为解决方法我在E4 RCP应用程序中使用以下代码:

  SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(false);
    }
  });

但我对它不满意,因为:

  • 我不知道它为什么会起作用
  • 我将Swing混合到纯SWT应用程序中
相关问题