SwingUtilities.invokeLater causing NoClassDefFoundError

Hello people,

I've built two classes using the Singleton Pattern in an application. The first class contains the application's main method, and the second is intended to provide some control over the main window application. The following code works correctly for me:

publicclass AppInit{

privatestatic AppInit instance =null;

private AppInit(){

< ...do some application initialization ...>

}

privatestatic AppInit getInstance(){

if( instance ==null) instance =new AppInit();

return instance;

}

publicstaticvoid main( String[] args ){

// Initiate the app services

instance =new AppInit();

// Building and exibiting GUI

AppWindow.getInstance().showWindow();

}

}

publicclass AppWindow{

privatestatic AppWindow instance =null;

private JFrame mainWindow =null;

private AppWindow(){

< ... instantiate and configure the mainWindow ...>

}

privatestatic AppWindow getInstance(){

if( instance ==null) instance =new AppInit();

return instance;

}

publicvoid showWindow(){

this.mainWindow.setVisible(true );

}

}

But reading about Swing, I've concluded that the GUI initialization should be done by the 'event dispatcher thread', so I changed the AppInit's main method to the following:

publicstaticvoid main( String[] args ){

// Initiate the app services

instance =new AppInit();

SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

// Building and exibiting GUI

AppWindow.getInstance().showWindow();

}

} );

}

This change results in a 'NoDefClassFoundError: AppInit$1', referencing the line of the invokeLater invocation.

I'm not sure if I should post this into this forum, or into the Concurrency forum. Anyway, if someone could give some explanation, or indicate where I can get the answers over this problem, I woul really apreciate that.

Thanks,

Alexandre

[4168 byte] By [alexandre_correaa] at [2007-11-26 18:11:23]
# 1

When you compile a ".java" file, it may produce more than one ".class" file. This occurs when you have inner classes.

The code you are using there produces an anonymous inner class ("AppInit$1").

Your classpath appears not to point to that class file - only your "AppInit.class" file.

itchyscratchya at 2007-7-9 5:43:55 > top of Java-index,Desktop,Core GUI APIs...
# 2

This has probably nothing to do with your current problem, but looking at your code more closely,

public class AppWindow {

private static AppWindow instance = null;

//...

private static AppWindow getInstance() {

if( instance == null) instance = new AppInit(); // <- new AppWindow()?

return instance;

}

//...

}

getInstance() should return an AppWindow reference. In addition, you call this inside AppInit.main(String[])

AppWindow.getInstance().showWindow();

but getInstance() has private access in AppWindow.

duckbilla at 2007-7-9 5:43:55 > top of Java-index,Desktop,Core GUI APIs...
# 3
Hi duckbill,thanks for the reply. That was just a typo error. I didn't copy and paste my code, only typed the main parts of it. In the real code it's new AppWindow().Best regards,Alexandre
alexandre_correaa at 2007-7-9 5:43:55 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thanks again for the help itchyscratchy,I was packaging my application without the inner class. Stupid error.Best regards,Alexandre
alexandre_correaa at 2007-7-9 5:43:55 > top of Java-index,Desktop,Core GUI APIs...