Search code examples
micronaut

Micronaut HelloWorld Error: Unexpected error: Illegal name .$HelloControllerDefinition


Trying to proceed the demo code on https://docs.micronaut.io/latest/guide/index.html#creatingServer

with the step following: src/main/java/example/helloworld/HelloController.java

came the error:

> Task :compileJava FAILED
Note: Creating bean classes for 1 type elements
error: Unexpected error: Illegal name .$HelloControllerDefinition
1 error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 7s
1 actionable task: 1 executed

i knew there was another question related this but has no answer error: Unexpected error: Illegal name .$HelloControllerDefinition

what should i do?

and the hellocontroller is exactly as the doc tells as follow

import io.micronaut.http.annotation.*;

@Controller("/hello") 
public class HelloController {
    @Get 
    public String index() {
        return "Hello World"; 
    }
}


Solution

  • HelloController needs to be defined to be in a package. If you followed the guide exactly, try the following,

    package example.helloworld;
    
    import io.micronaut.http.annotation.*;
    
    @Controller("/hello") 
    public class HelloController {
        @Get 
        public String index() {
            return "Hello World"; 
        }
    }