Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven can't compile using rt.jar

Tags:

java

maven

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?

like image 243
old_bear Avatar asked Jul 11 '15 04:07

old_bear


2 Answers

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.

like image 108
Gerold Broser Avatar answered Oct 06 '22 00:10

Gerold Broser


If you are using Gradle instead of Maven you can add this to your build:

compileJava {
    options.fork = true
    options.forkOptions.executable = 'javac'
    options.compilerArgs << "-XDignore.symbol.file"
}

It worked for me! ;)

like image 26
Leo Ribeiro Avatar answered Oct 05 '22 23:10

Leo Ribeiro