File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Basic Hello World Program not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic Hello World Program not working" Watch "Basic Hello World Program not working" New topic
Author

Basic Hello World Program not working

Pooja Prakash
Greenhorn

Joined: Jul 01, 2009
Posts: 9
I wrote a basic hello world program named Example.java . It compiled perfectly.But when i typed
C:\ java Example
java.lang.NoClassDefFoundError: Example
Caused by: java.lang.ClassNotFoundException: Example
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)
Could not find the main class: Example. Program will exit.


This is the problem i'm having.Someone please help.
Thanks in advance.
-Pooja
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

Details please. Where are your source files? Where are you class files? How are you compiling (working directory location and command line)? And how are you running (working directory location)? classpath?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Pooja Prakash
Greenhorn

Joined: Jul 01, 2009
Posts: 9
I have the source files in C:\ (ie C drive)
I compiled it in the same directory(so the class files are in the same directory as the source)
C:\ javac Example.java
( I experienced no problems with compilation)

-Pooja
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

And? .... There were more questions.

BTW, can you also show us the first few lines of the source too?

Henry
Pooja Prakash
Greenhorn

Joined: Jul 01, 2009
Posts: 9
I have set the path to C:\Program Files\Java\jdk1.6.0_14\bin
I tried executing the same program in Eclipse.I'm getting the same Exceptions.THe compilation's fine.


-Pooja
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

First, confirm that there is actually a Example.class file in the directory. If there isn't, then it didn't compile correctly.

Second, can you add "public" on the first line? Change the first line to "public class Example". And compile again.

Thanks,
Henry
Pooja Prakash
Greenhorn

Joined: Jul 01, 2009
Posts: 9
The .class file was there. I added "public" specifier and the program executed perfectly.Thanks a ton.
But when i tried executing it in Notepad++ it gave the exceptions i mentioned above. Is it because there's a problem in Notepad++??
As the program is working just fine in Textpad

-Thanks ,
Pooja
Carlos Segura
Greenhorn

Joined: Jun 29, 2009
Posts: 2
The problem is, you cannot declare a top class with any other scope besides public.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
Welcome to JavaRanch, Carlos Segura

You can declare a top-level class with public access, or miss out the modifier and use package-private access (default access).
Carlos Segura
Greenhorn

Joined: Jun 29, 2009
Posts: 2
You're completly right. And his code should work without any changes. I'm sorry about posting a mistake.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
Not so much a mistake, as imprecise grammar. You need to be careful, otherwise people can misunderstand what you say. You said that the only access (not "scope") modifier you can use on a top-level class is "public", which is absolutely true, but people then forget there is default access too.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Basic Hello World Program not working