• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My first "Hello World" is not working

 
Mario Cageggi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 HW.java with the following lines:
"
class HW {
public static void main (String[] args) {
System.out.println("HELLOWORLD");
}
}
"
and save it in the root.. for me c:\
Then I type in the command line:
c:\>javac HW.java
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 java.net.URLClassLoader$1.run(Unknown 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
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Campbell Ritchie
Sheriff
Posts: 48370
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 48370
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest has beaten me to it. It's usually Rob who does that
 
Mario Cageggi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48370
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic