Maven cannot compile with rt.jar

My project uses sun.security.tools.keytool to create a certificate in JDK 1.8, and this package can be found in rt.jar . According to an introduction to the dependency mechanism, system dependencies , I can add rt.jar as a dependency on 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 am sure Maven found this jar file. However, when I import sun.security.tools.keytool.Main , it still generates an error. Moreover, the strangest thing is, if I rt.jar to a place and fill its path in pom.xml , it works! As soon as I get back to use the original rt.jar , it does not work!

Can someone tell me how this could happen?

+3
java maven
source share
2 answers

I created a Maven project and added the <dependency> your question to my 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 Can't find the character (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> 

for POM, and compilation is complete.

+14
source share

If you use Gradle instead of Maven, you can add this to your assembly:

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

It worked for me!;)

+1
source share

All Articles