I have a test:
public class ResourceTest {
@Test
public void test() throws ClassNotFoundException {
Class.forName("javax.annotation.Resource");
}
}
javax.annotation.Resource
ClassNotFoundException
javax.annotation.Resource
module test {
requires java.xml.ws.annotation;
requires junit;
}
java.xml.ws.annotation
javax.annotation.Resource
requires
javax.annotations.Resource
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.1</version>
</dependency>
pom.xml
requires java.xml.ws.annotation
the unnamed module reads package javax.annotation from both java.xml.ws.annotation and java.annotation
java.xml.ws.annotation
requires
mvn clean test -DargLine="--add-modules java.xml.ws.annotation"
javax.annotation.Resource
Just to clear out some confusion here. The ways to work stated in the question by you are alternatives and should not be combined as you have already seen.
the unnamed module reads package javax.annotation from both java.xml.ws.annotation and java.annotation
So the way it would work is either:
You can use the compiler args to add modules
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<release>9</release>
<compilerArgs>
<arg>--add-modules</arg>
<arg>java.xml.ws.annotation</arg>
</compilerArgs>
</configuration>
</plugin>
Make use of the javax.xml.ws.annotation
being an upgradeable module which is when you can make use of the dependency
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.1</version>
</dependency>
Ideally this would be a preferrable option to stick to as the former is just an alternate to use the @Deprecated module marked forRemoval
.
So the required clause by itself it not enough to get access to a module... is this true for all JDK-supplied modules (excluding java.base), or it is only true for deprecated modules?
No, the requires
is just a part of declaration. [Think about this, prior to JDK 9 if you used a statement import some.foo.bar;
in your class which was not added as a library(classpath) would that have worked?]. The module marked as required has to be on the modulepath for you to access it.