<< Chapter < Page Chapter >> Page >
To run a Java program, the interpreter must be invoked with an argument giving the name of the main method. This module explains the exceptions that can be raised if the name is incorrect.

You can run a Java program from within an environment or by executing the interpreter from the command line:

java Test

where Test is the name of a class containing a main method.

Suggestion: It is convenient to write main methods in most classes and to use then for testing classes individually. After the classes are integrated into a singleprogram, you only need ensure that the interpreter is invoked on the class that contains the “real” main method.

Two runtime errors can occur if the interpreter is not successful in finding and running the program.

NoClassDefFoundError

The interpreter must be able to find the file containing a class with the main method, for example, Test.class . If packages are not used, this must be in the directory where theinterpreter is executed. Check that the name of the class is the same as the name of the file without the extension. Case is significant!

Warning! If you are compiling and running Java from a command window or shell with a history facility, you are likely to encounter this error. First,you compile the program:

javac MyClass.java

and then you recall that line and erase the c from javac to obtain the command for the interpreter:

java MyClass.java

Unfortunately, MyClass.java is not the name of the class so you will get this exception. You must erase the .java extension as well to run the interpreter:

java MyClass

If you are using packages, the main class must be in a subdirectory of that name. For example, given:

package project1.userinterface; class Test {  public static void main(String[] args) {    ...   }}

the file Test.class must be located in the directory userinterface that is a subdirectory of project1 that is a subdirectory of the directory where the interpreter is executed:

c:\projects> dir    <DIR> project1 c:\projects> dir project1    <DIR> userinterface c:\projects> dir project1\userinterface    Test.class

The program is invoked by giving the fully qualified name made up of the package names and the class name:

c:\projects> java project1.userinterface.Test

NoSuchMethodFoundError: main

This error will occur if there is no method main in the class, or if the declaration is not precisely correct: the static modifier, the void return type, the method name main written in lower case, and one parameter of type String[] . If you forget the public modifier, the error message is Main method not public .

Questions & Answers

what are components of cells
ofosola Reply
twugzfisfjxxkvdsifgfuy7 it
Sami
58214993
Sami
what is a salt
John
the difference between male and female reproduction
John
what is computed
IBRAHIM Reply
what is biology
IBRAHIM
what is the full meaning of biology
IBRAHIM
what is biology
Jeneba
what is cell
Kuot
425844168
Sami
what is biology
Inenevwo
what is cytoplasm
Emmanuel Reply
structure of an animal cell
Arrey Reply
what happens when the eustachian tube is blocked
Puseletso Reply
what's atoms
Achol Reply
discuss how the following factors such as predation risk, competition and habitat structure influence animal's foraging behavior in essay form
Burnet Reply
cell?
Kuot
location of cervical vertebra
KENNEDY Reply
What are acid
Sheriff Reply
define biology infour way
Happiness Reply
What are types of cell
Nansoh Reply
how can I get this book
Gatyin Reply
what is lump
Chineye Reply
what is cell
Maluak Reply
what is biology
Maluak
what is vertibrate
Jeneba
what's cornea?
Majak Reply
what are cell
Achol
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Compile and runtime errors in java. OpenStax CNX. Dec 28, 2009 Download for free at http://cnx.org/content/col10913/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Compile and runtime errors in java' conversation and receive update notifications?

Ask