• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Basic Hello World Program not working

 
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
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
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.
 
Marshal
Posts: 70228
282
  • 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
Marshal
Posts: 70228
282
  • 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.
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic