View Full Version : Java Programming - Help needed for Novice


D2J
07-12-2006, 20:12
Evening folks, Decided the other day I wanted to learn a programming language so have opted for Java and treated myself to a book.

I followed the instructions, downloaded the software etc and I'm following the book to the letter but the first program I did I get the error

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld

I have tried this three times I get the same message when I try to run it..

Any ideas what I should do next :huh:

LL200
07-12-2006, 20:25
wanna post us some code to take a look at? dont be disheartened... :)

D2J
07-12-2006, 20:30
I'm not disheartened, just eager to learn :)

This is what the book said to type in the file
/* HelloWorld.java
The classic first program
*/

public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello, World!");
}//end method definition
}//end class definition

LL200
07-12-2006, 20:31
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

save as HelloWorld.java. Then:

javac HelloWorld.java
java HelloWorld

Make sure you get the right case as Java is case-sensitive. Make sure you 'cd' into the correct directory before running the commands. Make sure you get a .class file after running the javac command.

LL200
07-12-2006, 20:32
String args[] is not the same as String[] args. Thats probably your problem :)

D2J
07-12-2006, 20:35
I did that before and it didn't work and still won't, maybe its my laptop :confused:

LL200
07-12-2006, 20:36
have you set up your classpath correctly?

D2J
07-12-2006, 20:39
have you set up your classpath correctly?

I think so, just to make sure what should I do :)

LL200
07-12-2006, 20:44
man its been so long since i played with java... :)

either:
java --classpath . HelloWorld

or:
set CLASSPATH=c:\wherever\i\put\my\code

if you've not done the second, do the first one when you run your code. the . simply means the current directory.

LL200
07-12-2006, 20:45
...and i'm now beginning to wonder if String[] args is the same as String args[]...

D2J
07-12-2006, 20:50
if you've not done the second, do the first one when you run your code. the . simply means the current directory.

The classpath was set to the second one, but I still get the same error. I think I'm having second thoughts now :lol:

Gonna write it again and see what happens :D

LL200
07-12-2006, 20:51
do you have spaces anywhere in the path? try moving your source code to c:\ and set your classpath to c:\ and run it from there.

alternatively, does it work if you do the first?

D2J
07-12-2006, 21:04
wrote it again, set the classpath to c:\wherever\i\put\my\code and I still get the same error when I try to run it..

When I installed the software I had to set the variable to the folder where it's installed

I'm still gonna keep trying :)

LL200
07-12-2006, 21:09
the classpath should be set to where 3rd party classes are installed, not to where java is installed. the classpath perhaps isnt the problem but its kinda hinting at it, if you ask me.

did you try:
cd to\where\i\put\my\code
javac HelloWorld.java
java --classpath . HelloWorld

because if that doesn't work then its not a classpath problem.

LL200
07-12-2006, 21:12
hmmm... try a single '-' rather than '--'

D2J
07-12-2006, 21:16
That doesnt work either, thanks for the help anyway :)

Im obviously hitting an error somewhere in the code..

D2J
07-12-2006, 21:20
:clap: :clap: :clap: :clap: works now! it wont work if I type java HelloWorld but works when I type java -classpath . HelloWorld

Thanks a million mate :thumbsup:

LL200
07-12-2006, 21:24
wooo i'm glad. so the classpath was the problem. i think -classpath is the better way rather than using set CLASSPATH as it means that you can put your code anywhere and not have to worry about adding the path to the CLASSPATH.

dont give up -- you're going to come across lots of things like this but there's a lot of people out there who are willing to help. its a good feeling when you get something working after battling with it for a while. it almost makes it all worthwhile :) almost...

D2J
07-12-2006, 21:25
Well thats Chapter 1 done, Applets next :?

:thumbsup:

I1L2T3
07-12-2006, 21:40
...and i'm now beginning to wonder if String[] args is the same as String args[]...


It is. String[] args is better style though

LL200
08-12-2006, 05:56
It is. String[] args is better style though

better style simply because it reads better? ("string array args" rather than string args array")

basshedz2
08-12-2006, 15:28
better style simply because it reads better? ("string array args" rather than string args array")

Yeah. I think the Java code conventions specify 'type[] name'. I was always told that its a matter of personal taste though. As long as your consistent.

And to D2J:

Install eclipse or netbeans - both are great. They make your life much easier by doing syntax highlighting and code completion.

I dont know what book you're using, but i would highly recommend Java Gently by Judy Bishop. Thats the book i learnt from and its a really good intro.

Final thing - I would also really recommend Python for anyone learning to program. Its easy, object oriented, and powerful.

b