Jump to content
  • Advertisement
Sign in to follow this  
SticksandStones

[java] Can't get program to run

This topic is 4966 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!