package myprojects.lol;
import java.awt.*;
import java.awt.event.*;
class Lol extends Frame {
public Lol() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public static void main(String args[]) {
System.out.println("Starting Lol...");
Lol mainFrame = new Lol();
mainFrame.setSize(400, 400);
mainFrame.setTitle("Lol");
mainFrame.setVisible(true);
}
}
What I use to try to run: java -cp . Lol; java -classpath . Lol, etc.
I get this error:
Quote:C:\Windows\Desktop\Programming files\Java>java -cp . Lol
Exception in thread "main" java.lang.NoClassDefFoundError: Lol (wrong n
ojects/lol/Lol)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Can anybody tell me why this isn't working? It works when I use the default loaded, and it works for other files. Any help?
hippopotomonstrosesquippedaliophobia- the fear of big words
I've been away from java for a few years now, but this code strikes me as entirely wrong. AFAIK it makes no sense to say "new Something" followed by curly braces which then definte a function... At a first pass i'm almost positive that even in java it is illegal to define functions within other functions... does this actually sucessfully compile?
Quote:
public Lol() { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); }
also, i believe that the first class definition has to also be public in order to be able to run the main function. i.e. i think it should be:
Yeah, you want to fully qualify the class name, i.e. "java -classpath . myprojects.lol.Lol". AFAIK anyway.
@Palidine: The code is valid. This is an example of an anonymous inner class. The class "Lol$1" (as it will be named/mangled by the compiler) extends WindowAdapter, overriding the windowClosing() method as indicated. (WindowAdapter is simply an implementation of the abstract WindowListener interface, providing empty implementations of the methods.) Since it's an inner class, every Lol$1 object is associated with a containing Lol object, and has access to its data (This isn't used here, however.) The full statement defines the Lol$1 class, instantiates an object thereof, and passes it to the Frame#addWindowListener() method.
This is all standard technique, and reads fairly elegantly. Although it would be more efficient to define a named WindowAdapter subclass externally, and instantiate that instead (since in this case we don't need the inner-class binding magic).