Forums Register Login

First Java Program not working

+Pie Number of slices to send: Send
hello, heres the code :

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'


using jdk1.4 on Win XP Pro

Ali
+Pie Number of slices to send: Send
Probelm is with the first line.. first two words

Change Public to public (p should be lowercase),
Change Class to class ( c should be lowercase).

cheers
Krishna
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

Ali
+Pie Number of slices to send: Send
Read section 2 of this.

Get the given example compiling and running and then apply what you learn to your own example.
+Pie Number of slices to send: Send
hi

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.

cheers
Anandh
+Pie Number of slices to send: Send
replace your line with this:

public static void main(String[] args)

not



public static void main(String [] args)
+Pie Number of slices to send: Send
a space between the String and the [] does not matter -- both will compile and run, as will "String args[]"
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Hello,

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.

Ali
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Hello,

Kindly do explain what do u mean by Classpath. Detail the whole process if possible thanks

Ali
+Pie Number of slices to send: Send
just try

java -cp "." OurFirstProgram
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
You may find this page helpful to get you started.
+Pie Number of slices to send: Send
IT works, the -cp statment. Can someone explain what '-cp "." ' means?

Also when i ran some other programs afterwards, i didnt need to type the -cp statement, typing just 'java' worked. why is that?

Also, i have jdk1.4, should i get the j2sdk1.4???

Ali
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
P.S. jdk 1.4 and j2sdk 1.4 are the same thing.
+Pie Number of slices to send: Send
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
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
https://gardener-gift.com


reply
reply
This thread has been viewed 943 times.
Similar Threads
Running Java Programs from the command prompt
Running Java Program
Multiple instances of java class
Java Program Without main() method
Running compiled code
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:27:53.