aspose file tools*
The moose likes Beginning Java and the fly likes error in the first program... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "error in the first program..." Watch "error in the first program..." New topic
Author

error in the first program...

Nitin Menon
Ranch Hand

Joined: Jun 13, 2007
Posts: 79
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

Joined: Nov 18, 2003
Posts: 179
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

Joined: Jun 06, 2007
Posts: 2716
    
    6

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


SCJP, SCWCD.
|Asking Good Questions|
Nitin Menon
Ranch Hand

Joined: Jun 13, 2007
Posts: 79
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

Joined: Oct 02, 2003
Posts: 11419
    
  16

have you set a classpath variable? from the directory where your .class file is, try:

java -cp . a

(i think i have that right)


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Are you using netbeans or are you doing this from the command line?
Nitin Menon
Ranch Hand

Joined: Jun 13, 2007
Posts: 79
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

Joined: Oct 02, 2003
Posts: 11419
    
  16

"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

Joined: Jun 13, 2007
Posts: 79
I ran the command..!

the output was typed...

Hello World!
Nitin Menon
Ranch Hand

Joined: Jun 13, 2007
Posts: 79
I ran the command..!

the output was typed...

Hello World!
Nitin Menon
Ranch Hand

Joined: Jun 13, 2007
Posts: 79
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

Joined: Oct 02, 2003
Posts: 11419
    
  16

"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

Joined: Jun 13, 2007
Posts: 79
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: error in the first program...