C:\mywork>java hello
Exception in thread "main" java.lang.NoClassDefFoundError: hello (wrong name: Hello)
This run-time error (exception) may happen when you mistype a lower case letter for upper case. Normally a class name (e.g., Hello) starts with an upper case letter and the file name should be the same. SDK 1.3 under Windows will compile a file hello.java that defines a class Hello, but when you try to run it as above, it reports an exception.
C:\mywork>java Hello
is correct.
C:\mywork>java Hello.java
Exception in thread "main" java.lang.NoClassDefFoundError: Hello/java
or
C:\mywork>java Hello.class
Exception in thread "main" java.lang.NoClassDefFoundError: Hello/class
The command to run the Java interpreter should use the class name but should not include any extension, neither .java nor .class. An extension in the file name confuses the interpreter about the location of the class (the extension is interpreted as a subfolder).
C:\mywork>java Hello
Exception in thread "main" java.lang.NoSuchMethodError: main
This exception may be reported when the main method is missing or its signature is incorrect. The correct signature is
public static void main (String[] args)
Possible mistakes: private static void main (String[] args)
public void main (String[] args)
public static int main (String[] args)
public static void main (String args)
Hello.java:12: non-static method printMsg(java.lang.String) cannot be
referenced from a static context
printMsg(s);
^
The keyword static is missing in the printMsg header:
public void printMsg(String msg)
should be: public static void printMsg(String msg)
Since main is a static method and it calls printMsg with no "something-dot" prefix, printMsg is assumed to be another static method of the same class.
Hello.java:7: cannot resolve symbol
symbol : class EasyReader
location: class Hello
EasyReader console = new EasyReader();
^
Unless installed as a package and properly imported, files for classes used in the program (in this case EasyReader.java or EasyReader.class) should be available in the same folder as Hello.java. A cannot resolve class error may also show up when a library class is not imported or when data type is incorrect or misspelled. For example:
private bool match(String word, int row, int col, int rowStep, int colStep)
gives WordSearch.java:32: cannot resolve symbol
symbol : class bool
location: class WordSearch
private bool match(String word, int row, int col, int rowStep, int colStep)
^
It should be boolean.
Hello.java:7: cannot resolve symbol
symbol : method PrintMsg (java.lang.String)
location: class Hello
PrintMsg(s);
^
This error may occur when a method is called incorrectly: either its name is misspelled (or upper-lower case is misplaced), or a method is called with wrong types of arguments, or a method is called for a wrong type of object or a wrong class. For example, the same error, will be reported if you write
System.println("Hello");
instead of System.out.println("Hello");
Another example: Hello.java:20: cannot resolve symbol
symbol : method println (java.lang.String,java.lang.String)
location: class java.io.PrintStream
System.out.println("You entered: ", msg);
^
Here a comma is used instead of a + in the println call. This makes it a call with two arguments instead of one and no println method exists that takes two String arguments.
Hello.java:8: '}' expected
}
^
An extra opening brace or a missing closing brace may produce several errors, including
Hello.java:13: illegal start of expression
public static void printMsg(String msg)
^
Hello.java:16: ';' expected
}
^
and finally Hello.java:17: '}' expected
}
^
which may be reported at the end of the file.
Hello.java:3: <identifier> expected
static x;
^
<identifier> expected is a rather common error message. Here x is a variable, but the compiler thinks it is a class name. It is the data type designation that's actually missing. It should be:
static <someType> x;
The same happens here: private myRows, myCols;
It gives an error: WordSearch.java:4: <identifier> expected
private myRows, myCols;
^
thinking that myRows is a data type. Same here: public static void printMsg(msg)
{
...
}
- a missing type designator (e.g., String) in a method's header produces four rather obscure errors: Hello.java:19: <identifier> expected
public static void printMsg(msg)
^
Hello.java:29: ')' expected
}
^
Hello.java:19: cannot resolve symbol
symbol : class msg
location: class Hello
public static void printMsg(msg)
^
Hello.java:19: missing method body, or declare abstract
public static void printMsg(msg)
^
4 errors
It should be: public static void printMsg(String msg)
WordSearch.java:18: incompatible types
found : int
required: boolean
if (i = n)
^
It is supposed to be
if (i == n)
Single = makes it assignment operator. It returns an int value that can't be tested in if. Similarly, return row = 0 && row < myRows && col >= 0 && col < myCols;
gives two errors: WordSearch.java:29: incompatible types
found : int
required: boolean
return row = 0 && row < myRows && col >= 0 && col < myCols;
^
WordSearch.java:29: operator && cannot be applied to int,boolean
return row = 0 && row < myRows && col >= 0 && col < myCols;
^
2 errors
An extraneous space between ! and = in an != operator may give several errors, including "incompatible types": Hello.java:10: ')' expected
if (s ! = null)
^
Hello.java:14: illegal start of expression
}
^
Hello.java:13: ';' expected
}
^
Hello.java:10: incompatible types
found : java.lang.String
required: boolean
if (s ! = null)
^
4 errors
Another situation with "incompatible types" is when a literal string is used in place of a char constant or vice-versa. For example: WordSearch.java:21: incompatible types
found : java.lang.String
required: char
grid[r][c] = "*";
^
Should be grid[r][c] = '*';