I have a test:
public class ResourceTest {
@Test
public void test() throws ClassNotFoundException {
Class.forName("javax.annotation.Resource");
}
}
It tries to access javax.annotation.Resource
. In java 8 it worked, but in java 9 (I'm using Oracle JDK 9) it fails with ClassNotFoundException
.
As explained here Spring: @Resource injection stopped working under JDK9 , javax.annotation.Resource
from the JDK is not available by default in Java 9.
I'm trying to get access to it using module descriptor:
module test {
requires java.xml.ws.annotation;
requires junit;
}
Here, I specifically request access to java.xml.ws.annotation
module (which contains javax.annotation.Resource
). But the test still fails.
When I remove that requires
clause and add a dependency (as a library) which contains javax.annotations.Resource
, it works:
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.1</version>
</dependency>
When I add them both (Maven dependency in pom.xml
and requires java.xml.ws.annotation
), compilation in IDEA fails with the following message:
the unnamed module reads package javax.annotation from both java.xml.ws.annotation and java.annotation
But Maven build still succeeds!
If I add java.xml.ws.annotation
module via command line, it works (with no Maven dependency and with requires
clause):
mvn clean test -DargLine="--add-modules java.xml.ws.annotation"
Do I do something wrong with my module description? How can I get access to JDK-supplied javax.annotation.Resource
without command line switches?
The test project is available at https://github.com/rpuch/test-resource-jdk9
--add-modules
argline and thenrequires <modulename>
? – Exenteraterequires java.annotation
and the Maven dependency on javax.annotation/javax.annotation-api/1.3.1 as you have done. This eliminates the--add-modules
from the usage too. – Ossifrage