How to fix "illegal start of expression" error in Java

The "illegal start of expression" error is a compile time error when the compiler finds an inappropriate statement in the code. The java compiler, javac, compiles your source code from top to bottom, left to right and when it sees something inappropriate at the start of an expression, it throws "illegal start of expression" error. The most common reason of this is a missing semi-colon. You might know that every statement in Java ends with a semicolon, but if you forget one, you won't get an error that there is a missing semi-colon at the end of statement because the compiler doesn't know the end. When compiler checks the next statement it sees illegal start because an earlier statement was not terminated. The bad part is that you can get tens of "illegal start of expression" error by just omitting a single semi-colon or missing braces, as shown in the following example.

public class Main {

  public static void main(String[] args) {

  public static int count() {
    return 0;


If you compile this program you will be greeted with several "illegal start of expression" error as shown below:

$ javac error: illegal start of expression
  public static int count() {
  ^ error: illegal start of expression
  public static int count() {
         ^ error: ';' expected
  public static int count() {
               ^ error: ';' expected
  public static int count() {
                           ^ error: reached end of file while parsing
5 errors

And the only problem is a missing braces in main method, as soon as you add that missing closing curly brace in main method the code will work fine and all these error will go away, as shown below:

How to fix "illegal start of expression" error in Java

The error message is very interesting, if you look at the first one, you will find that error is in line 7 and compiler complaing about public keyword but actual problem was in line 5 where a closing brace is missing. From compiler's view, public should not come there becase its an invalid keyword inside a method in Java, remember without closing braces main() is still not closed, so compiler think public keyword is part of the main() method.

Though, when you compile the same program in Eclipse, you get different error because it's compiler is slightly differnet than javac but the error message is more helpful as you can see below:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error, insert "}" to complete MethodBody

at Main.main(

In short, the "illegal start of expression" error means compiler find something inappropriate, against the rules of Java programming but the error message is not very helpful. For "illegal start of expression" errors, try looking at the lines preceding the error for a missing ')' or '}' or missing semicolon. Also remember, a single syntax error somewhere can cause multiple "illegal start of expression" error. Once you fix the root cause, all error will go away. Which means always recompile once you fix an error, don't try to make multiple changes without compilation.

Other Java troubleshooting guides you may like:
  • How to fix "variable might not have been initialized" error in Java? (solution)
  • Could not create the Java virtual machine Invalid maximum heap size: -Xmx (solution)
  • How to fix class, enum, or interface expected error in Java? (solution)
  • How to fix java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory (solution)
  • Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger in Java (solution)
  • Error: could not open 'C:\Java\jre8\lib\amd64\jvm.cfg' (solution)
  • java.lang.OutOfMemoryError: Java heap space : Cause and Solution (steps)
  • How to avoid ConcurrentModificationException while looping over List in Java? (solution)


No comments:

Post a Comment