• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not able to run a class file

 
Dino Yan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Java. I installed JDK and tried to execute a simple HelloWorldApp example (as suggested in the Sun.java tutorial) and i get this below error. can any one help?

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp
Caused by: java.lang.ClassNotFoundException: HelloWorldApp
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Several people have had the same problem today; assuming you have a little application and have not deleted anything (see the API documentation), it means the .class file you think you are running isn't where you have told the java program to look.

Have you got a package declaration in your .java file (you probably haven't)?
Did the .java file compile correctly? You can tell when it compiles correctly; nothing appears on screen.
Give an "ls" (Linux/Unix) or "dir" (Windows) command and see whether the HelloWorldApp.class file appears. Check it doesn't say helloWorldApp.class or anything like that; the tiniest spelling error will stop it working.

You ought to create a "java" directory somewhere in "home" or "My Documents" or somewhere else accessible and put all your Java work in that. Save the .java files there, then use the cd command to navigate there with the command line. Then javac then java.
Those stages are shown clearly in the Sun Tutorial page you mentioned.


Good luck with it.

[edit]Added "have not" before "deleted"[/edit]
[ October 15, 2008: Message edited by: Campbell Ritchie ]
 
Dino Yan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell, really appreaciate your quick response.

I think it compiled correctly as i see the HelloWorldApp.class file.
but when i execute it, it says java HelloWorldApp it give me the error
As you suggested i checked for spell errors and i didn't see any.

Also other than setting PATH should i be configuring something more?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you set a system classpath?

Try running as...

Note the spaces on each side of the dot in the above command. If this works, then you have set a system CLASSPATH that does not include a dot (.) for the current directory.
 
Dino Yan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc, that worked. Thanks a lot!

What should i set the class path to.

C:\javaE is where i am storing all my java files

C:\java\jre..\ is where i have java installed.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can set the CLASSPATH in a similar fashion to setting the PATH. I presume you are on Windows; I usually use Linux and there doesn't seem to be anything in a Linux CLASSPATH at all.

Find the existing system CLASSPATH and put .; (dot-semicolon) at its very beginning. If there isn't a CLASSPATH at all create a new one containing ;.; (semicolon-dot-semicolon). See how that works.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on your OS, you might need to reboot for the change to take effect.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I forgot you might need to reboot.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I question that statement.

It isn't necessary for Windows. You will have to close and reopen any command window though, because these read the environment variables upon start.

It isn't necessary for Linux. You will have to reload your profile / shell config file though, but with bash "source <file>" can do that. And even if you can't reload it, logging off and logging back in is still faster than rebooting.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is NOT a good thing to add your programs to the system CLASSPATH property. That classpath is always searched first by the JVMs. Imagine that you have two programs that uses two different versions of the same third party library, and you point to the earlier one on the system classpath...

Instead, always use the -cp or -classpath flags on the java command.

I usually create a .bat-file or more often now, an ANT-script that handles the build and execution of the program.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree, don't add your folders to the system CLASSPATH, but the . for current directory ought to be in there, at least on Windows.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
I question that statement.

It isn't necessary for Windows...

It depends on the version. I know rebooting is required for Windows ME (which I admit to using years ago). I thought there was another version that also required rebooting, but maybe ME is the oddity.

For most versions of Windows running today, rebooting should not be needed.
 
Dino Yan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help guys! i was able to run the file.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

It depends on the version. I know rebooting is required for Windows ME (which I admit to using years ago). I thought there was another version that also required rebooting, but maybe ME is the oddity.

For most versions of Windows running today, rebooting should not be needed.

Ah yes, you're right. ME and everything before (a.k.a. the DOS based Windows versions) were quite annoying with the number of reboots required. Changed the IP address? Reboot! Changed AUTOEXEC.BAT (the place to go for variables, ugh!)? Reboot! Changed the hostname? Reboot! Oh wait, XP still has that last one

There's a joke going around about that:
Windows has detected that your mouse has moved. Restart?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
There's a joke going around about that:

"Windows has detected that your mouse has moved. Restart?"

Nice!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic