This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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)
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.
Added "have not" before "deleted"[/edit] [ October 15, 2008: Message edited by: Campbell Ritchie ]
Joined: Oct 15, 2008
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?
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.
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.
Joined: Oct 13, 2005
Agree, don't add your folders to the system CLASSPATH, but the . for current directory ought to be in there, at least on 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.
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?