Compiling and Running The Java Program And Troubleshooting The Compilation

0




Compiling and Running the TestGreeting Program

After you have created the TestGreeting.java source file, compile it with the following line:

javac TestGreeting.java

If the compiler does not return any messages, the new file TestGreeting.class is stored in the same directory as the source file, unless specified otherwise. The Greeting.java file has been compiled into Greeting.class. This is done automatically by the compiler because the Test Greeting class uses the Greeting class.

To run your TestGreeting application, use the Java technology interpreter. The executables for the Java technology tools (javac, java, javadoc, and so on) are located in the bin directory.

java TestGreeting

Note – You must set the PATH environment variable to find java and javac; ensure that it includes java_root/bin (where java_root represents the directory root where the Java technology software is installed).

Troubleshooting the Compilation

The following sections describe errors that you might encounter when compiling code.

Compile-Time Errors

The following are common errors seen at compile time, with examples of compiler or runtime messages. Your messages can vary depending on which version of the Java 2 SDK you are using.

javac: Command not found

Greeting.java:4:cannot resolve symbol

The PATH variable is not set properly to include the javac compiler. The javac compiler is located in the bin directory below the installed Java Development Kit (JDKTM) software directory.

symbol : method printl (java.lang.String)

location: class java.io.PrintStream

System.out.printl(“hi”);

The method name println is typed incorrectly.

● Class and file naming

If the .java file contains a public class, then it must have the same file name as that class. For example, the definition of the class in the previous example is:

public class TestGreeting

The name of the source file must be TestGreeting.java. If you named the file TestGreet.java, then you would get the error message: TestGreet.java:4: Public class TestGreeting must be defined in a file called “TestGreeting.java”.

● Class count

You should declare only one top-level, non-static class to be public in each source file, and it must have the same name as the source file. If you have more than one public class, then you will get the same message as in the previous bullet for every public class in the file that does not have the same name as the file.

Runtime Errors

Some of the errors generated when typing java TestGreeting are:

● Can’t find class TestGreeting

Generally, this means that the class name specified on the commandline was spelled differently than the filename.class file. The Java programming language is case-sensitive.

For example,

public class TestGreet {

creates a TestGreet.class, which is not the class name (TestGreeting.class) that the compiler expected.

● Exception in thread “main” java.lang.NoSuchMethodError: main

This means that the class you told the interpreter to execute does not have a static main method. There might be a main method, but it might not be declared with the static keyword or it might have the wrong parameters declared, such as:

public static void main(String args) {

In this example, args is a single string, not an array of strings.

public static void main() {

In this example, the coder forgot to include any parameter list.

Figure illustrates how Java technology programs can be compiled and then run on the JVM. There are many implementations of the JVM on different hardware and operating system platforms.

3