Source File Must Be Named to Match a Public ClassYou get a compiler error stating that a public class must be declared in a file with a certain name . When you declare a public class, such as HelloWorld or HelloApplet in the examples found in this chapter, the name of the source file that contains the class must match the class name. For example, a public class named HelloWorld must be stored in a file named HelloWorld.java. NoSuchMethodError: mainYou get a runtime error identifying a NoSuchMethodError related to the main method. When you execute a Java application, the interpreter attempts to call the main method on the class identified as the starting point for the program. This error indicates that either the class specified in the java command does not contain a main method or the method it contains is not declared correctly. Your main method must be declared using the following syntax: public static void main(String args[]){ NoClassDefFoundErrorYou attempt to run a program and you get an error stating that the class definition cannot be found. The Java interpreter executes a program by first loading the class file corresponding to the name you provide to the java command. For example, when you type java HelloWorld, HelloWorld.class is loaded and executed. A NoClassDefFoundError indicates that the specified class file cannot be located in the current classpath. First, locate the class file and make sure that you spelled the class name correctly and typed it using the proper case. If this is correct, it is likely that class file is not in your classpath. Refer to Appendix A, "Installing the SDK and Getting Started," for more information on setting the classpath. See "The CLASSPATH Environment Variable" IOExceptionYou get a compiler error stating that java.io.IOException must be caught or declared to be thrown. If you are calling System.in.read within one of this chapter's examples, the call must be placed inside a try-catch block that handles java.lang.IOException. Refer to Listing 2.4 for the proper syntax of this try-catch block. |