I keep getting this message when i'm trying to compile the "helloworld!".java file, the message is as follows; Exception in thread "main" java.NoClassDefFoundError HelloWorld. any suggestions welcome, god help me i can't even compile the first program
Hi Robert, welcome to JavaRanch. Can't answer for god, but a bunch of other folks will be glad to try and help. The error you got, "java.NoClassDefFoundError HelloWorld", means that the compiler can't find your HelloWorld.java file. Where have you saved HelloWorld.java? In the instructions, there's this step: I put all of my stuff into a directory called java (off of my root, C:\, in Windows and off of my home directory in unix). On my windows machine, my classpath is set to "C:\j2sdk1.4\LIB;.;C:\JAVA" Did you get through that step OK?
Originally posted by robert woolley: I keep getting this message when i'm trying to compile the "helloworld!".java file, the message is as follows; Exception in thread "main" java.NoClassDefFoundError HelloWorld. any suggestions welcome.
Your question is a little confusing.
You compile with the "javac" command. It won't give the error you mention, although you might see "'javac' is not recognized as an internal or external command, operable program or batch file" if your jdk is not installed properly or your PATH is not set properly. The PATH environment variable is something completely different and independent of the CLASSPATH variable that Pauline mentioned above.
The error you reference is seen when you try to run the program using the "java" command. Something like: java HelloWorld
Remember that if you saved your file as HelloWorld.java and and compiled it as javac HelloWorld.java you must run it as java HelloWorld
java helloworld won't work. Neither will java helloworld.java or java helloworld.class
Are you in the same subdirectory that your file is in when you're trying to run it? If your HelloWorld.class file is in C:\Java, then you should be in that subdirectory when you type java HelloWorld. There are other ways, but this is the simplest way. [ September 04, 2003: Message edited by: Marilyn de Queiroz ]
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Joined: Sep 04, 2003
i have sorted it out thanks for the help people. i can know start programming, i think it was a mixture of me not being in the right directory and using javac and java the wrong way around but it's all clear know. Thanks
Originally posted by John Hembree: I think Pauline hit it right on the head.
You definitely need the ;.; in your classpath.
Why is ;.; needed in the classpath? Is there any significance where it is positioned? [ September 07, 2003: Message edited by: sherry cuenco ]
Joined: Jan 19, 2001
As I understand it (not a Windows offionado), the . stands for "current directory". So if you're doing your java or javac from C:myStuff\javaStuff it will look for classes in C:myStuff\javaStuff (along with any other places listed in the classpath). The ; just separates one path from the next one. I don't think the order of the paths makes any difference in practice, though it might be that the paths are checked in that order.
Marilyn de Queiroz
Joined: Jul 22, 2000
Theoretically, you should not need the '.' in your classpath anymore, although many people still recommend its use. As Pauline said, it stands for the "current" directory (subdirectory) that you are in at the time of the command.
Pauline also hit it right on the head about the paths being searched in the order that they are in the classpath. I've heard that one shouldn't put the '.' first in the classpath because it makes it easier to catch a virus, but I've never seen any documentation of that.
Joined: May 07, 2003
Thanks Marilyn and Pauline.
So if you're doing your java or javac from C:myStuff\javaStuff it will look for classes in C:myStuff\javaStuff (along with any other places listed in the classpath).
Let's say C:\myStuff\javaStuff is in the classpath. If I create new subfolders and store my classes there: C:\myStuff\javaStuff\OOP C:\myStuff\javaStuff\OOP\Solution C:\myStuff\javaStuff\servlets C:\myStuff\javaStuff\servlets\Solution C:\myStuff\javaStuff\JDBC I was expecting the compiler would automatically detect the classes stored in these new subfolders. Since it did not, I had to add them individually in my classpath. I thought maybe adding a '.' would do the trick
Marilyn de Queiroz
Joined: Jul 22, 2000
That is not the function of the '.'
However, you could change the directory (cd C:\myStuff\javaStuff\OOP) before typing like this:
or you could work from the root directory like this:
(which is a lot more typing)
or you could use packages (packages will be discussed in future assignments). [ September 07, 2003: Message edited by: Marilyn de Queiroz ]