Error Message when running first java program
Hi,
I am reading "Sams Teach Yourself Java 6 in 21 Days" by Rogers Cadenhead. I am having a problem with compiling the sample program in the first part of the book, "HelloUser" which is supposed to print out "Hello (my name - Matt)" when run in the command line.
I receive this error when trying to run it after having successfully compiled it (I enter java HelloUser while in correct folder):
Exception in thread "main" java.lang.NoSuchMethodError: main
I have correctly set the PATH and CLASSPATH settings for my PC (Windows XP). I am guessing that I need to define a "main" or something. I'm brand new to Java but am good with the scripting language PHP. Please tell me what I should do to correct this problem. Thanks in advance
Hey Sierra Dog / Matt and welcome here. Why don't you post your code so we can see it. Please use code tags if you do. See this site to learn more about code tags:
http://forum.java.sun.com/help.jspa?sec=formatting
Also, double check that you have a method called main (note that this has to be all in lower-case). It should look exactly like the method in the book. So remember precision is critical as the compiler is unforgiving. Case and spelling matter.
Good luck!
Here is the code for the program:
public class HelloUser {
public static void man(String[] arguments) {
String username = System.getProperty("user.name");
System.out.println("Hello " + username);
}
}
Here is what I enter in the Command Line:
cd \JavaWork\j21work
javac HelloUser.java
java HelloUser
There are no problems up until I enter the last line (java HelloUser) to run the program. By the way, I using JDK 6 update 1 with Windows XP.
No problem, but I didn't do anything. You had the best result here that you can expect: you found the error yourself and then corrected it yourself. There's no better way to learn than this. Because of this, I can guarantee that this error will never happen to you again.
Good luck with your coding!