Java – Maven can’t compile using rt.jar

javamaven

My project uses sun.security.tools.keytool to generate certificate under JDK 1.8 and this package can be found in rt.jar. According to Introduction to the Dependency Mechanism, System Dependencies, I can add rt.jar as a dependency to my project:

<dependency>
  <groupId>sun.jdk</groupId>
  <artifactId>rt.jar</artifactId>
  <version>1.8</version>
  <scope>system</scope>
  <systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>

I'm pretty sure Maven found this jar file. However when I import sun.security.tools.keytool.Main, it still generates an error. Moreover, the most strange thing is if I copy rt.jar into someplace and fill its path in pom.xml, it works! As soon as I switch back to use the original rt.jar, it fails!

Can anyone tell me how could this happen?

Best Answer

I created a Maven project and added the <dependency> of your question to its POM.

First I got:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] .../SO-31353565/src/main/java/igb/so/SO31353565.java:[6,34]
    package sun.security.tools.keytool does not exist
[ERROR] .../SO-31353565/src/main/java/igb/so/SO31353565.java:[12,50]
    cannot find symbol
  symbol:   variable Main
  location: class igb.so.SO31353565

Then, according to Cannot find symbol (CertAndKeyGen) with JDK8, I added:

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <fork>true</fork>
          <compilerArgument>-XDignore.symbol.file</compilerArgument>
        </configuration>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

to the POM and the compilation succeeded.