[java] Can't get program to run

Started by
5 comments, last by Zahlman 19 years, 1 month ago
Code:
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
Advertisement
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:

public class Lol extends Frame {

-me
on second glance, my guess would be that the error means you haven't compiled it yet. have you run "javac yourfilename.java" yet?

-me
Yes, it has been compiled.
hippopotomonstrosesquippedaliophobia- the fear of big words
I'm pretty sure a similar "feature" caught me out a while back - I just can't remember the exact solution [smile]

package myprojects.lol;
Is probably causing you problems.

When you execute "java" you need to qualify the package that its in, or it will assume its in the root/default (unnamed) package.

It's getting late and I can't remember exactly how it goes, but:
java myprojects.lol
or
java myprojects\lol

if those dont work, look up the exact commands in the spec (or google) and/or try similar combinations.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Ok...works now...recompiled several times...yeah :(

I think I need a computer update ><
hippopotomonstrosesquippedaliophobia- the fear of big words
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).

This topic is closed to new replies.

Advertisement