JAVA

Common Errors in Java

Common Error Messages on Microsoft Windows Systems

Command Prompt
javac is not recognized as an internal or external command, operable program or batch file

If you receive this error, Windows cannot find the compiler (javac).

Here's one way to tell Windows where to find javac. Suppose you installed the JDK in C:\jdk1.8.0. At the prompt you would type the following command and press Enter:

Command Prompt
C:\jdk1.8.0\bin\javac TutorialinkApp.java

Syntax Errors

If you mistype part of a program, the compiler may issue a syntax error. The message usually displays the type of the error, the line number where the error was detected, the code on that line, and the position of the error within the code. Here's an error caused by omitting a semicolon (;) at the end of a statement:

Command Prompt
testing.java:7: ;' expected.
System.out.println("Input has " + count + " chars.")
                                                    ^
1 error

Sometimes the compiler can't guess your intent and prints a confusing error message or multiple error messages if the error cascades over several lines. For example, the following code snippet omits a semicolon (;) from the bold line:

Command Prompt
while (System.in.read() != -1)
    count++
System.out.println("Input has " + count + " chars.");

When processing this code, the compiler issues two error messages:

Command Prompt
testing.java:6: Invalid type expression.
        count++
               ^
testing.java:7: Invalid declaration.
    System.out.println("Input has " + count + " chars.");
                                          ^
2 errors

The compiler issues two error messages because after it processes count++, the compiler's state indicates that it's in the middle of an expression. Without the semicolon, the compiler has no way of knowing that the statement is complete.

If you see any compiler errors, then your program did not successfully compile, and the compiler did not create a .class file. Carefully verify the program, fix any errors that you detect, and try again.


Semantic Errors

In addition to verifying that your program is syntactically correct, the compiler checks for other basic correctness. For example, the compiler warns you each time you use a variable that has not been initialized:

Command Prompt
testing.java:6: Variable count may not have been initialized.
        count++
        ^
testing.java:7: Variable count may not have been initialized.
    System.out.println("Input has " + count + " chars.");
                                       ^
2 errors

Again, your program did not successfully compile, and the compiler did not create a .class file. Fix the error and try again.

Runtime Problems

Error Messages on Microsoft Windows Systems

Command Prompt
Exception in thread "main" java.lang.NoClassDefFoundError: TutorialinkApp

If you receive this error, java cannot find your bytecode file, TutorialinkApp.class.

One of the places java tries to find your .class file is your current directory. So if your .class file is in C:\java, you should change your current directory to that. To change your directory.

Could not find or load main class TutorialinkApp.class

A common mistake made by beginner programmers is to try and run the java launcher on the .class file that was created by the compiler. For example, you'll get this error if you try to run your program with java TutorialinkApp.class instead of java TutorialinkApp. Remember, the argument is the name of the class that you want to use, not the filename.

Exception in thread "main" java.lang.NoSuchMethodError: main

The Java VM requires that the class you execute with it have a main method at which to begin execution of your application. 




Subscribe us on Youtube

Share This Page on

Question


Bhavesh Vadgama | 02-Jul-2016 05:33:16 pm

Still, m' using x64 based OS and tried vary to install and setup connection drivers [JDBC+ODBC], but i can't be able to execute programs instead including external mysql-connectors.jar library. Is there anyone to give help for mah query?


Ask Question