Results 1 to 9 of 9
  1. #1
    siji44 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default 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...

  2. #2
    karno is offline Member
    Join Date
    Mar 2010
    Posts
    16
    Rep Power
    0

    Default

    may you add some of your coding ?

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  4. #4
    siji44 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default

    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....

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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!).

  6. #6
    siji44 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default

    The both (filename and classname) are same..

  7. #7
    siji44 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default

    now its working...
    I have no idea how this happens.

    I think the classpath must be c:\Java\jre\bin.

    Thank you everyone.

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    No. The classpath must point to your class files not the jre bin.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by siji44 View Post
    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)
    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

  1. HelloWorld in EJB!
    By bdtagar in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 01-04-2010, 11:55 AM
  2. Struts2 helloworld application.
    By makpandian in forum New To Java
    Replies: 0
    Last Post: 06-02-2009, 11:19 AM
  3. About Java HelloWorld
    By Dankydoo in forum New To Java
    Replies: 0
    Last Post: 01-11-2009, 10:08 PM
  4. Run a helloWorld in NetBeans
    By mathias in forum NetBeans
    Replies: 1
    Last Post: 08-07-2007, 01:16 AM
  5. Problem with my program HelloWorld
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 05:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •