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;
Code Java:
// 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.
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.
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)
Code Java:
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.
Re: i keep getting this error
For a class named Helloworld.class, try "java Helloworld"
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!:eek::eek::eek::eek:
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.:cool: