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