Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve java error: package javax.annotation is not visible, Java version 9

When building below Spring session sample using gradle: https://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot-findbyusername.html

I encountered an error regarding to java.annotation module, anybody have idea how to resolve that?

/spring-session/spring-session-core/src/main/java/org/springframework/session/config/annotation/web/http/SpringHttpSessionConfiguration.java:22: error: package javax.annotation is not visible
import javax.annotation.PostConstruct;
            ^
  (package javax.annotation is declared in module java.xml.ws.annotation, which is not in the module graph)
warning: unknown enum constant When.MAYBE
  reason: class file for javax.annotation.meta.When not found
1 error
1 warning

FAILURE: Build failed with an exception.

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

I tried to add below config in build.gradle however the problem remain.

tasks.withType(AbstractCompile) {
  options.compilerArgs += ["--add-modules", "java.xml.bind"]
}

tasks.withType(Test) {
  jvmArgs += ["--add-modules", "java.xml.bind"]
}
like image 566
Ryan Cy Avatar asked Nov 26 '22 01:11

Ryan Cy


1 Answers

I was getting the same error:

warning: unknown enum constant When.MAYBE
reason: class file for javax.annotation.meta.When not found

For me it was becuase I had annotation processing with lombok which wasn't picked-up with the project dependencies.

In the end I added annotationProcessor as part of the dependencies as follows:

dependencies {
  // ...
  compile('org.springframework.boot:spring-boot-starter-web')
  compileOnly('org.projectlombok:lombok')
  testCompile('org.springframework.boot:spring-boot-starter-test')
  // ...
  annotationProcessor('org.projectlombok:lombok')
}
like image 196
Ithar Avatar answered Dec 28 '22 08:12

Ithar