This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Not able to run a class file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Not able to run a class file" Watch "Not able to run a class file" New topic
Author

Not able to run a class file

Dino Yan
Greenhorn

Joined: Oct 15, 2008
Posts: 4
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

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Oct 15, 2008
Posts: 4
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Dino Yan
Greenhorn

Joined: Oct 15, 2008
Posts: 4
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

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Aug 31, 2004
Posts: 11343

Depending on your OS, you might need to reboot for the change to take effect.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Yes, I forgot you might need to reboot.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

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

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Oct 15, 2008
Posts: 4
Thanks for all the help guys! i was able to run the file.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Rob Prime:
There's a joke going around about that:

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

Nice!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to run a class file
 
Similar Threads
Error by compiling textfile Could not find the main class: HelloWorldapp. Program will exit
errors with main class
Fresh SDK Install Won't Compile?
Why I can't run Web Start when my class using hibernate?
MySQL Connector/J