Please help with the most basic thing!

Started by
6 comments, last by Kevinator 18 years, 2 months ago
I am trying this simple little program but it just doesn't want to run. This program compiles, and when i run it gives me message: ------------------------------------------------------------------------ Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld Press any key to continue... ------------------------------------------------------------------------ HelloWorld.java

import javax.swing.*;

public class HelloWorld
{
	public static void main (String []args)
	{
		JOptionPane.showMessageDialog(null, "Hello World");
	}// end main
}


main is right there!!!!... I don't understand thank you
Advertisement
make sure your classpath includes the current directory...

java -classpath . HelloWorld

Are you using an IDE or notepad/commandline? do other java programs work? The reason I ask is i just ran your exact code in intelliJ and it worked like a charm, as it should. Maybe environment variables in windows isn't set?
I'm no java guru, but is that the full command for JOptionPane? I thought it was something more like
JOptionPane.showMessageDialog(null,"HELLO WORLD", "Title", JOptionPane.INFORMATION_MESSAGE);

Again, i'm not sure that is the problem, but sometimes compilers can be picky - so who knows. It can't hurt to try.
Quote:Original post by DJHoy
make sure your classpath includes the current directory...

java -classpath . HelloWorld


how do I make sure of that. I am using "textpad 4.7.3" by the way

How do I set the "environment variables in windows "?

And actually other programs don't work either, like:
Hello.java
public class Hello{	public static void main (String [] args)	{		System.out.println ("Hello World");	}}

Gives the same error
Quote:Original post by snm04
How do I set the "environment variables in windows "?

See step 5 of the Java 2 SDK 1.4.2 Installation Notes for Microsoft Windows [java.sun.com/].

Make sure that the path is actually correct for your system when following the instructions - the example one may not exactly match your own if you have a different version of the SDK or a different install location on your system for example, in which case just pasting in the one in the article wouldn't work properly.

- Jason Astle-Adams

You have to specify your entry class. In your second example the class containing main is Hello. So you would have to run:

java Hello

Searching for some simple java tutorials will give you any answers you need.
Hit the start button, then click on run, then type in cmd, then hit enter, then go to the directory with Hello.java in it. Then run javac.exe on Hello.java, then run java.exe and give it an argument of Hello, and it WILL run, I guarantee it.

This topic is closed to new replies.

Advertisement