I upgraded the SDK that my project uses to Java 10. The following import statement caused an error:
import javax.annotation.PostConstruct;
Package 'javax.annotation' is declared in module 'java.xml.ws.annotation', but module 'application' does not read it
Hitting ALT+ENTER to let Intellij fix it, I received the following options:
I selected Add Maven Dependency...
and the following dependency was added to pom.xml.
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
Looking at the newly added JAR's MANIFEST.MF, I noted that the module's name was java.annotation.
So i added requires java.annotation
to module-info.java.
module application {
requires java.annotation;
}
When I CTRL+clicked on requires java.annotation
:
Intellij correctly navigated to the JAR:
So it appeared that I had the module name correct and that javax.annotation:javax.annotation-api
was available to my module. Unfortunately, the error on import javax.annotation.PostConstruct
did not go away. To confirm that my module descriptor was working properly, I added other directives, and all worked fine. I also added the directive requires java.xml.ws.annotation
,
which made the import statement error go away but, of course, this is not a satisfactory solution as java.xml.ws.annotation is deprecated and marked for removal.
Any ideas on how to resolve this problem? I need to be able to make module java.annotation
available to module application
and from what I've read here and here and here and here the way to do it is by adding a JAR to the module path, and from what I've read here the newly added module must be referenced via directive in module-info.java.
requires java.xml.ws.annotation
to moduleapplication
– Cumberlandrequires java.xml.ws.annotation
, and although it resolved the error in my import statement I did not want to use it because it's deprecated and marked for removal. – Schleswigholstein--add-modules
on the command line, to add the module to the module path at runtime. IntelliJ almost certainly has an equivalent of this, but I don’t know what it is. – Lind--add-modules
option is the quick and dirty solution that will no longer work when Java 11 is released, and should not be used if at all possible. The proper long-term solution in the linked question and answers is to instead include the module on the classpath or module path, and the way to do that is to add the Maven dependencyjavax.annotation:javax.annotation-api
to pom.xml and to reference the module in module-info.java, which is what I did. Unfortunately the error did not go away. – Schleswigholsteinrequires
directive) differs. Could you clarify which one are you using? – Zygophyllaceousimport javax.annotation.PostConstruct
statement. I selected theAdd Maven Dependency...
option, which modified pom.xml as shown. Next, I added therequires
directive to module-info.java. I CTRL+clicked on therequires
directive, which drilled through to the JAR added to the classpath by the newly created dependency in pom.xml. The second screenshot shows the JAR. – Schleswigholstein