View Full Version : Java Programming - Help needed for Novice
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:
wanna post us some code to take a look at? dont be disheartened... :)
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
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.
String args[] is not the same as String[] args. Thats probably your problem :)
I did that before and it didn't work and still won't, maybe its my laptop :confused:
have you set up your classpath correctly?
have you set up your classpath correctly?
I think so, just to make sure what should I do :)
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.
...and i'm now beginning to wonder if String[] args is the same as String args[]...
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
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?
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 :)
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.
hmmm... try a single '-' rather than '--'
That doesnt work either, thanks for the help anyway :)
Im obviously hitting an error somewhere in the code..
: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:
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...
Well thats Chapter 1 done, Applets next :?
:thumbsup:
...and i'm now beginning to wonder if String[] args is the same as String args[]...
It is. String[] args is better style though
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
|