What is the cause and resolution of java.lang.ExceptionInInitializerError error when trying to open a JFrame in another Thread?
I'm trying to creating a test class to open a JFrame. In order to stop the window from closing the moment the main thread finishes I added the code to open up the window in another thread. Every time I run the application I get the following exception:
Exception in thread "Test Thread" java.lang.ExceptionInInitializerError at java.lang.Runtime.addShutdownHook(Runtime.java:192) at java.util.logging.LogManager.(LogManager.java:237) at java.util.logging.LogManager$1.run(LogManager.java:177) at java.security.AccessController.doPrivileged(Native Method) at java.util.logging.LogManager.(LogManager.java:158) at java.util.logging.Logger.getLogger(Logger.java:273) at java.awt.Component.(Component.java:173) at reflector.ApplicationRunner.startObjectsPool(ApplicationRunner.java:18) at reflector.ReflectorEndToEndTest$1.run(ReflectorEndToEndTest.java:20) Caused by: java.lang.IllegalStateException: Shutdown in progress at java.lang.Shutdown.add(Shutdown.java:62) at java.lang.ApplicationShutdownHooks.(ApplicationShutdownHooks.java:21) ... 9 more
The code is below:
@Test
public void createIntegerClass() throws Exception {
Thread t = new Thread("Test Thread") {
@Override
public void run() {
try {
application.startObjectsPool();
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
t.setDaemon(true);
}
public class ApplicationRunner {
public final static String[] NO_ARGS = null;
public void startObjectsPool() throws Exception {
ObjectsPoolFrame frame = new ObjectsPoolFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
public开发者_Python百科 ObjectsPoolFrame() {
setTitle("Objects Pool");
// get screen dimension
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
// center frame in screen
setSize(screenWidth / 2, screenHeight / 2);
setLocation(screenWidth / 4, screenHeight / 4);
op = new ObjectPool();
// add buttons on the top
j1 = new JButton("Create Object");
j2 = new JButton("Delete Object");
j3 = new JButton("Display Methods");
j4 = new JButton("Invoke Method");
JPanel buttonPanel = new JPanel();
buttonPanel.add(j1);
buttonPanel.add(j2);
buttonPanel.add(j3);
buttonPanel.add(j4);
add(buttonPanel, BorderLayout.NORTH);
j1.addActionListener(new CreateObjectAction());
j2.addActionListener(new DeleteObjectAction());
j3.addActionListener(new DisplayMethodAction());
j4.addActionListener(new InvokeMethodAction());
comboBox = new JComboBox();
comboBox.addActionListener(new ComboBoxClearAction());
addComboBoxItem();
comboBox2 = new JComboBox();
JPanel comboPanel = new JPanel();
comboPanel.add(new JLabel("Objects"));
comboPanel.add(comboBox);
comboPanel.add(new JLabel("Methods"));
comboPanel.add(comboBox2);
add(comboPanel, BorderLayout.CENTER);
displayMessage = new JLabel();
JPanel displayPanel = new JPanel();
displayPanel.add(displayMessage);
add(displayPanel, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
I can't figure why I'm getting the issue.
The exception message tells you exactly what is wrong: you're trying to create a new thread while the JVM is shutting down.
The reason the JVM shuts down when the main thread finishes is because you call setDaemon(true)
on your event thread. Remove that line and the JVM will stay up as long as that thread is alive.
the problem occurs when you try to set a Thread to Daemon(true). When you exit your application the thread cannot be stopped and hence throws an java.lang.IllegalStateException: Shutdown in progress
when you came over this problem you have to explicitly tell the Runtime to close the thread anyway, by adding a shotdownhook.
public void createIntegerClass() throws Exception {
Thread t = new Thread("Test Thread") {...};
Runtime.getRuntime().addShutdownHook(t);//explicit!
t.start();
t.setDaemon(true);
}
精彩评论