Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error in the first program...

 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, iam a biginner to core java programming. i installed the jdk 1.5 which has net beans 4 included. after installation, i set the path correctly. then, i tried the very first java program. i comliled te program and no errors turned out. but, apon running, the following error occured. please help me.

Exception in thread "main" java.lang.NoClassDefFoundError: a

the program is :-



class a {

public static void main (String args[]) {

System.out.println("Hello World!");

}

}

the file name is a.java.

cant understand wat the problem is. plzz help me...
thanks in advance.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means the a.java is not vible, meaning a class is not in loaded to classpath.
Also learn to use Java API:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/NoClassDefFoundError.html
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Originally posted by Nitin Ram

Exception in thread "main" java.lang.NoClassDefFoundError: a

the program is :-



Here there is a simple mistake that the class a declared above is not public thats why you are getting an error in executing it.
Remember that the filename and the public class name must be identical always

Hope this helps
 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
is it, that you mean - instead of class a I should give public class a?
its still giving the same problem. the very same error. i tried running the same program in net beans 4 and its working. Its not working using notepad and command prompt.
please help me.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you set a classpath variable? from the directory where your .class file is, try:

java -cp . a

(i think i have that right)
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using netbeans or are you doing this from the command line?
 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it about setting the class path to the bin folder in the jdk folder?
this is the location of the bin folder in my system.
my O.S is windows XP and how i did it is by right clicking my computer and
then - advanced tab-environment variables-system variables. here, i added the below link to the currently existing paths.
D:\Program Files\Java\jdk1.5.0_04\bin

i guess the class path set is right because, if not, i guess the javac and java wont work.
lpease throw light on the above....
thank you very much in advance...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"path" and "classpath" are completely separate (note: there is no such thing as a class path variable). "path" tells the OS where to look for the executables, such as java.exe and javac.exe.

"classpath" tells JAVA where to look for your .class files.

the -cp flag tells java where to look for the classfiles. the "." means look in the current directory.

so, if you are at a command prompt, go to the directory where your class file lives (it should be called "a.class").

then, run the command

java -cp . a


tell us what happens.
 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran the command..!

the output was typed...

Hello World!
 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran the command..!

the output was typed...

Hello World!
 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear Fred,

why am i not able to get the out put without giving -cp and "."?
is it that that the JVM automatically looks in some other location? i compiled and run the program from the location of the file through command prompt.
please help me as i've done this program before without any problems...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"classpath" is a system variable that tells java where to look for your class files. If you have it set (and you can check the same way you checked what your 'path' variable was set), java only looks there for your class files. if you classpath is

classpath = c:/working/project/foo;c:/working/project/bar;

if your class files are in ANY other directory, java can't find them. i THINK (i am not sure on this) if you don't have a classpath set, it defaults to the current directory.

My guess is that you have a classpath set, and that your a.class file is not in one of those listed directories. by putting the -cp flag on, you are telling the JVM to look in the directory you specify. since you put a '.', you tell it to look in the current directory. it won't work if you cd up, down, or over to a different directory.

you can, however, do

java -cp c:/this/is/where/my/class/files/are a

if that is indeed the path to your class files.

so, a solution would be to add a dot to your classpath. then, java will know to look in the current directory, no matter what that current directory is. a more complicated (and better) solution would be to add the path where your class files actually live into your classpath.
 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear Fred,
thank you for your helpful guidance. i am still facing the problem. anyway, the programs are working fine in net-beans version 4 I d e. i have included the class path with the location of the .class files.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic