Public Class OurFirstProgram { public static void main (String [] args) { System.out.println ("Hello World"); } }
I save the program as OurFirstProgram.java and using javac convert it to .class. But then why i type 'java OurFirstProgram' an error message is given :
'Exception in thread "main" java.lang.NoclassDefFoundErrors : OurFirstProgram'
Yes, make sure that when you compile your classes you check the output from the compiler for errors and warnings. If this is how it was in the file (capitals on public and class), you should have received a compiler error telling you it didnn't understand "Public".
Therefore, when you tried to run the compiled class, Java told you it couldn't find it because it hadn't been compiled successfully.
Thankyou for the reply. However the caps was only in this posting. the program is error free, and the jdk reported no preoblems, and as stated the .class file was compiled successfully. However it is not being executed. Please advise.
i have a question for you... is that space between 'String' and '[]' present in the program also? if yes, that could be a problem. remove that space and try it out.
This problem I've run into on every computer I've tried to install Java on in a while. It's a serious pain in the ass, and it boggles my mind that it's not a more commonly adressed issue, with a clear cut solution out there. One thing I've found to sometimes work, is to go into the control panel, System, Advanced tab, Environment Variables, and delete the variable for classpath. Find someone cleverer than me to tell you why you did that, but it seems to work.
I did delete the space between String and [] however that does not make a difference, the system does not run any class file. i tried a number of different examples from the book and Net yet i still get the same error. Please advise.
The problem is with your CLASSPATH settings. Your CLASSPATH variable should have the current working directory also. Add a dot (.) to the CLASSPATH, so that the current working directory is always a part of your CLASSPATH.
Classpath is a list of directories or jars where the JVM should look for classes. Just like path in good ole dos it has semicolons between directories.
You can set classpath as an environment variable, either at the system configuration level or in a batch file just as you run Java. Or you can set it as in the example above with the -cp argument to java. I have batch files to launch most of my own Java programs and they set up the classpath just for that one program.
Have you learned about packages yet? If not, I don't want to jump ahead. Get this running and come back for packages.
the classpath is the set of folders that your computer will look for to run things. In this case the place you saved your java file wasn't in any of the paths. If your on Windows the classpath is under the environment variables button of the system properties.
the reason it worked with -cp "." is the -cp flag tells the compiler to add paths to the class path(but only for that run) and the "." is the symbol for the current directory.
The other programs you ran must have been on your classpath.
If you start using an IDE, like eclipse, you can usually get away from these more common classpath problems(although it can come up on other situations, such as using other libraries).
hi ali change the upper case letter of P & C of public & class to its lowercase respectively.if you are using commandprompt(cmd)to compile and run the program,make sure that you have typed the class name correctly(including upper & lower letter cases(eg javac OurFirstProgram.java for compiling and java OurFirstProgram for running. if u typed the copiling command as javac ourfirstprogram.java it may compile but it wont run showing exception) ).
public class OurFirstProgram { public static void main (String [] args) { System.out.println ("Hello World"); } }
regards Abhilash
Post by:autobot
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
a bit of art, as a gift, the permaculture playing cards