Results 1 to 9 of 9
Thread: How to Run HelloWorld program?
- 03-24-2010, 04:53 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
How to Run HelloWorld program?
Hi,
I just write my first java program HelloWorldApp.java
But when i run it, im getting error like this....
C:\Java\jb\Ch04>java HelloWorldApp
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp (wrong
name: jb/ch04/HelloWorldApp)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknow n Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
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)
Could not find the main class: HelloWorldApp. Program will exit.
I set my CLASSPATH.
set CLASSPATH=.;C:\Java;C:\Java\lib
But still im getting the same error. Can anybody help, Please...
- 03-24-2010, 06:12 AM #2
Member
- Join Date
- Mar 2010
- Posts
- 16
- Rep Power
- 0
may you add some of your coding ?
- 03-24-2010, 06:30 AM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Did you compile the file into a .class file first?
What package is the class in? Did you create a package structure that matches the package name? After doing all this, you then provide the path to the .class file with the -cp flag to the java command.
- 03-24-2010, 06:39 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
This is my HelloWorldApp.java
package jb.ch04;
import java.lang.System;
class HelloWorldApp{
public static void main(String args[]){
System.out.println("Hello World!");
}
}
I created directories jb\ch04 under java dir and saved the above file.
In the command prompt I did
C:\Java\jb\Ch04>javac HelloWorldApp.java
C:\Java\jb\Ch04>java HelloWorldApp
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp (wrong
name: jb/ch04/HelloWorldApp)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknow n Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
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)
Could not find the main class: HelloWorldApp. Program will exit.
I set Classpath firstly as
.; C:\Java\lib
and secondly as
C:\Java\jre\bin
Still Im getting the same error....
- 03-24-2010, 06:55 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
What name does your class have? It should be the same as the filename. For example, if your filename is HelloWorldApp.java, your class should be called public class HelloWorldApp (java is case sensitive!).
- 03-24-2010, 06:59 AM #6
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
The both (filename and classname) are same..
- 03-24-2010, 07:15 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
now its working...
I have no idea how this happens.
I think the classpath must be c:\Java\jre\bin.
Thank you everyone.
- 03-24-2010, 07:31 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
No. The classpath must point to your class files not the jre bin.
- 03-24-2010, 08:09 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
I think for now it's better not to store your class in any package (except the default package). First study about the classpath variable and what packages actually imply. First remove that "package jb.cho4;" statement from your source code and try again.
kind regards,
Jos
Similar Threads
-
HelloWorld in EJB!
By bdtagar in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 01-04-2010, 11:55 AM -
Struts2 helloworld application.
By makpandian in forum New To JavaReplies: 0Last Post: 06-02-2009, 11:19 AM -
About Java HelloWorld
By Dankydoo in forum New To JavaReplies: 0Last Post: 01-11-2009, 10:08 PM -
Run a helloWorld in NetBeans
By mathias in forum NetBeansReplies: 1Last Post: 08-07-2007, 01:16 AM -
Problem with my program HelloWorld
By trill in forum New To JavaReplies: 1Last Post: 08-05-2007, 05:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks