Intellij: how to add java.annotation module for javax.annotation.PostConstruct
Asked Answered
S

1

8

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

enter image description here

Hitting ALT+ENTER to let Intellij fix it, I received the following options:

enter image description here

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.

enter image description here

So i added requires java.annotation to module-info.java.

module application {
    requires java.annotation;
}

When I CTRL+clicked on requires java.annotation:

enter image description here

Intellij correctly navigated to the JAR:

enter image description here

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,

enter image description here

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.

enter image description here

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.

Schleswigholstein answered 29/8, 2018 at 19:21 Comment(9)
Try to add requires java.xml.ws.annotation to module applicationCumberland
I should have mentioned that I added requires 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
Essentially a duplicate of #43574926.Lind
@Lind I did read over that question several times prior to submitting my question and the answer https://mcmap.net/q/22042/-how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception is exactly the solution I aimed for. Unfortunately whereas that answer worked for a Spring Boot project, it did not work for my project using Intellij. Adding the needed artifact to pom.xml did not resolve the problem.Schleswigholstein
I don’t use IntelliJ, but I assume IntelliJ did not add that module to the runtime module path. Merely adding the declaration module-info.java and making it available at compile-time isn’t sufficient. As the linked answer states, you need --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
The --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 dependency javax.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.Schleswigholstein
@PatrickGarner The screenshot and the actual module descriptor (requires directive) differs. Could you clarify which one are you using?Zygophyllaceous
@nullpointer The first screenshot displays the options I was presented with when I hit ALT+ENTER on the import javax.annotation.PostConstruct statement. I selected the Add Maven Dependency... option, which modified pom.xml as shown. Next, I added the requires directive to module-info.java. I CTRL+clicked on the requires 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
I updated the question for clarity.Schleswigholstein
P
10

The trouble with IntelliJ IDEA is that it knows about the module in JDK/jmods/java.xml.ws.annotation.jmod, even though at runtime it will be disabled. Just comment out the java.xml.ws.annotation jmod in the Java SDK definitions in IntelliJ IDEA Project Structure.

Detail steps:

Add a Maven dependency:

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

and module-info dependency:

requires java.annotation;

as you already did.

Then, stop IntelliJ IDEA, go to your IntelliJ JDK config file (i.e. C:\Users\YOUR_NAME\.IntelliJ2018\config\options\jdk.table.xml and comment out the line with java.xml.ws.annotation.jmod reference:

<!-- <root url="jrt://C:/Java-Training/JDK!/java.xml.ws.annotation" type="simple" /> -->

IntelliJ will stop seeing java.xml.ws.annotation. After restart, everything will be fine.

Polyneuritis answered 24/10, 2018 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.