This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes My first Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "My first "Hello World" is not working" Watch "My first "Hello World" is not working" New topic

My first "Hello World" is not working

Mario Cageggi

Joined: Apr 22, 2009
Posts: 8
Hi guys!

I downloaded from the Sun website the file "jdk-6u13-windows-i586-p.exe" it should be the Java developement kit installer for Windows (I use XP).
I installed it, and added the bin directory to the path of the system, so I can use the javac and java command everywhere.
I wrote through notepad the file with the following lines:
class HW {
public static void main (String[] args) {
and save it in the root.. for me c:\
Then I type in the command line:
and it looks work fine, because it generate the HW.class file w/o any error message.
Then I type:
c:\>java HW
and it is not working;
I tried also:
c:\>java HW.class
and in both case I get the following error message:
Exception in thread "main" java.lang.NoClassDefFoundError: HW
Caused by: java.lang.ClassNotFoundException: HW
at$ Source)
Could not fine the main class: HW. Program will exit.

Where am I wrong???
I feel so dumm..
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24193

Hi Mario,

Welcome to JavaRanch!

Java uses an environment variable named "CLASSPATH" to tell it where to look for class files. If the variable is not set at all, Java will look in the current directory, which is generally what you want. If it's set, and doesn't include an entry for "." (dot, representing the current directory) then the current directory won't be searched. Often some installer will have "helpfully" set CLASSPATH for you; QuickTime for Windows is one common culprit.

In any case, just tell Java explicitly to look in the current directory:

java -classpath . HW

That's "java space dash classpath space dot space HW".

[Jess in Action][AskingGoodQuestions]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44616
Welcome to JavaRanch

Don't put your files into the root of the drive. Create yourself a "java" folder with the mkdir instruction on the command prompt, then put all your files in there.
Don't use NotePad for programming; on Windows try jEdit, NotePad2 or NotePad++; they are far better, supporting all sorts of helpful features like syntax colouring and bracket matching. They also don't have the nasty habit which NotePad has of adding .txt to filenames.

But none of that explains why you are getting errors.

You appear to have correctly installed Java and set up the PATH (otherwise you would have got a different error message).
I can see no errors in the code you quoted.

Please check carefully: Have you made any spelling errors? Even the tiniest error will produce such problems.

Try java . HW

If that works: As well as setting the PATH, did you simultaneously set the CLASSPATH? If you did, delete the CLASSPATH environment variable altogether. If you didn't set a CLASSPATH, edit it and see whether it contains a . If it doesn't, edit the CLASSPATH by adding .; at its beginning (you can add the . anywhere but it will be slightly faster at the beginning).
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44616
Ernest has beaten me to it. It's usually Rob who does that
Mario Cageggi

Joined: Apr 22, 2009
Posts: 8
To Ernest:

Thanks a lot for the advices! It worked immediately! Finally I'd say!

To Ritchie:
jejeje! I know notepat is a crap to write code and I should not use the root for my programs... I was just superexcited to run my first Java code that I couldn't wait!
And thanks also for explaining me that I needed to set the CLASSPATH variable as well!

Lucas Franceschi
Ranch Hand

Joined: Nov 10, 2008
Posts: 106

well, but you wouldn't like to put all the cirectory for your classes in the classpath will you?

i'll assume no.

so its interesting for you to put a "." entry in classpath

try puttin' the string "." in your classpath.

if you're in windows, you can do this:
go to "my computer" properties>advanced>environment variables
then look for the CLASSPATH variable you setted before, and put ".;" before any ohther directory.

doing so will tell java to look always in the directory from where java.exe was called, so that instead of putting every directory on the classpath, you can simply make the java call for your program from the directory where are you're classes.

Lucas Franceschi
Software Developer for SGI Sistemas,
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44616
Well done
I agree. Here's the link:
subject: My first "Hello World" is not working
It's not a secret anymore!