Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes First Java Program not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "First Java Program not working" Watch "First Java Program not working" New topic
Author

First Java Program not working

Ali Gilani
Ranch Hand

Joined: Mar 01, 2002
Posts: 137
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
Krishna Mohan V
Greenhorn

Joined: Mar 16, 2004
Posts: 20
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
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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.
Ali Gilani
Ranch Hand

Joined: Mar 01, 2002
Posts: 137
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
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Read section 2 of this.

Get the given example compiling and running and then apply what you learn to your own example.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Anandh Ramesh
Ranch Hand

Joined: Dec 15, 2004
Posts: 61
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


cheers,<br />Anandh
david lightman
Ranch Hand

Joined: Nov 03, 2004
Posts: 82
replace your line with this:

public static void main(String[] args)

not



public static void main(String [] args)
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

a space between the String and the [] does not matter -- both will compile and run, as will "String args[]"


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
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've heard it takes forever to grow a woman from the ground
Ali Gilani
Ranch Hand

Joined: Mar 01, 2002
Posts: 137
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
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
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.


Mani
Quaerendo Invenietis
Ali Gilani
Ranch Hand

Joined: Mar 01, 2002
Posts: 137
Hello,

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

Ali
Thomas Rochon
Ranch Hand

Joined: Jul 11, 2002
Posts: 72
just try

java -cp "." OurFirstProgram
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
You may find this page helpful to get you started.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ali Gilani
Ranch Hand

Joined: Mar 01, 2002
Posts: 137
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
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
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).
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
P.S. jdk 1.4 and j2sdk 1.4 are the same thing.
Abhilash Sasi
Greenhorn

Joined: Nov 24, 2004
Posts: 6
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: First Java Program not working