如何解决Java错误:Java版本9中看不到包javax.annotation

时间:2018-07-28 03:19:14

标签: java gradle spring-session

在使用gradle构建Spring会话示例以下时: https://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot-findbyusername.html

我遇到有关java.annotation模块的错误,有人知道如何解决吗?

/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.

我试图在build.gradle中的config下面添加,但是问题仍然存在。

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

tasks.withType(Test) {
  jvmArgs += ["--add-modules", "java.xml.bind"]
}

2 个答案:

答案 0 :(得分:0)

我遇到了同样的错误:

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

对我来说,这是因为我使用lombok进行了注释处理,但没有依赖项目依赖项。

最后,我添加了annotationProcessor作为依赖项的一部分,如下所示:

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')
}

答案 1 :(得分:-2)

似乎该样本尚未升级为支持java9,因此降级为java8可以很好地运行。