Re: i get java.lang.ClassNotFoundException wher i run my application
from JWS
Andrew Thompson wrote:
A variety of time, discussions and experiences
later, I tried again using the /CCL/ in main()
and finally, was able to manage the 'marvellous
technology' of finding an URL to a frame icon,
without sub-classing frame.
You might want to take a look at this:
<http://www.javaworld.com/javaworld/javaqa/2003-06/01-qa-0606-load.html>
The important bit is on the second page, where it says that there is no
single right answer. It depends on how the system (and CCL) is configured.
CCLs are intended as a back-door for frameworks where the regular class
loader won't work. It's not an all singing, all dancing solution to
resource loading. In fact, I'm surprised it works in an applet, since
the default is for a CCL to be set to the system class loader, which we
saw in the OP's example that didn't work.
Here's a bit of code I whipped up that loads an image icon without using
a CCL, or sub-classing a JFrame. I'll post a complete Jar file in a bit.
package frameimage;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Main {
public static void main( String[] args ) throws IOException
{
SwingUtilities.invokeLater( new Runnable() {
public void run()
{
createAndShowGui();
}
} );
}
private static void createAndShowGui()
{
JFrame frame = new JFrame();
JLabel message = new JLabel( "Frame with icon." );
message.setFont( new java.awt.Font( "Tahoma", 0, 36 ) );
frame.add( message );
java.net.URL imgURL = Main.class.getResource(
"images/Favorites.png" );
BufferedImage img = null;
try {
img = ImageIO.read( imgURL );
}catch( IOException ex ) {
Logger.getLogger( Main.class.getName() ).log( Level.SEVERE,
null, ex );
}
frame.setIconImage( img );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
}
}