Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic Hello World Program not working

 
Pooja Prakash
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pooja Prakash
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And? .... There were more questions.

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

Henry
 
Pooja Prakash
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is, you cannot declare a top class with any other scope besides public.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're completly right. And his code should work without any changes. I'm sorry about posting a mistake.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic