[Solved] ”IllegalStateException: cannot run from EDT” while calling main() with reflection

Hi,



I’m trying to create a class that is able to search for other classes in it’s directory, then lists them in a JComboBox so that the user can choose which application to load. Everything works except for the application loadeing. I’m trying to simply call the main() method which results in an InvocationTargetException which is caused by:


java.lang.IllegalStateException: Cannot run from EDT
at com.jme3.system.JmeSystem.showSettingsDialog(JmeSystem.java:105)
at com.jme3.app.SimpleApplication.start(SimpleApplication.java:128)
at examples.AnotherTestApp.main(AnotherTestApp.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at examples.ExampleChooser.actionPerformed(ExampleChooser.java:74)
[...]

The actionPerformed method looks like this:
[java]public void actionPerformed(ActionEvent e) {
try{
Class.forName("examples."+(String)box.getSelectedItem()).getMethod(
"main", String[].class).invoke(null, (Object)new String[0]);
} catch(Exception ex) {
// Exception logging
}
}[/java]
Where line 74 is the one beginning with "Class.forName". It's a little bit ugly but it should work. All the classes are in the package examples, the class names are stored without the examples and running the main() method from command line works fine.
Since the "cannot run from EDT" error seems to be JME specific I'm posting this here.
I know that there's another thread dealing with the same Exception but it's a year old and I'm not sure if it's really the same topic.

What is going wrong?

Well, it seems you’re trying to start the application from the EDT (Event dispatching thread).

Perhaps try starting a new thread to run it from?

1 Like

Oh well yes, that worked. Now that I think about it, I can see why it didn’t work. Maybe it was simply to late yesterday. Thank you.