• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

First Java Program not working

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read section 2 of this.

Get the given example compiling and running and then apply what you learn to your own example.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
replace your line with this:

public static void main(String[] args)

not



public static void main(String [] args)
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a space between the String and the [] does not matter -- both will compile and run, as will "String args[]"
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ali Gilani
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ali Gilani
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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

Ali
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just try

java -cp "." OurFirstProgram
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may find this page helpful to get you started.
 
Ali Gilani
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. jdk 1.4 and j2sdk 1.4 are the same thing.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic