Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: i keep getting this error

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy i keep getting this error

    plz, i'm new to this Java. i've installed jDK 1.6 and configured the Environment Variable yet each time i compile and try to run a Hello World program in DOS, i get this message;
    D:\javafiles>javac Helloworld.java
    Helloworld.java:3: class HelloWorld is public, should be declared in a file name
    d HelloWorld.java
    public class HelloWorld {
    ^
    1 error
    here is the actual program;
    // A program to display the message
    // "Hello World!" on standard output
    public class HelloWorld {
    public static void main(String[] args) {
    System.out.println("Hello World!");
    }
    } // end of class HelloWorld
    Plz any help rendered will be highly appreciated.
    Last edited by helloworld922; September 23rd, 2011 at 10:59 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: i keep getting this error

    java is case sensitive. Check your cases in the class name and the file name - they should be identical.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: i keep getting this error

    i changed the root drive from d: to C:
    then tried to compile and run this: (it ran perfectly in Eclipse platform, but i would love to learn Java with JDK)
    public class Hello {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    System.out.println ("Hello World!");
    	}
     
    }
    it compiled and created a Helloworld.class file in the folder, but gives me this error message when i try to run using the 'java Helloworld.java' command
    C:\javafiles>java helloworld.java
    Exception in thread "main" java.lang.NoClassDefFoundError: helloworld/java
    Caused by: java.lang.ClassNotFoundException: helloworld.java
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47)
    Could not find the main class: helloworld.java. Program will exit.
    Last edited by helloworld922; September 23rd, 2011 at 11:00 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: i keep getting this error

    For a class named Helloworld.class, try "java Helloworld"

  5. The Following User Says Thank You to copeg For This Useful Post:

    okey (September 24th, 2011)

  6. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Wink Re: i keep getting this error

    hi dudie,
    see since you have declared the class as public therefore by the rule
    "in java when a class is declared public then the program has to be saved with the name of the class which has been declared as public.
    in your case you have named public class HelloWorld,
    therefore you should save the file with the name HelloWorld.java.
    remember the 'H' and 'W' of HelloWorld should be in capital"

    this will surely work!
    go ahead and try it out.
    HAVE A NICE-DAY!

  7. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: i keep getting this error

    God! it worked like black magic...lol Copeng and shang2392, thanks guys. i appreciate. i just created my first java program with JDK. eclipse seems easier but i choose the rocky road less traveled. java here i come.