6

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?

4
  • 2
    rt.jar contains the runtime classes - so it shouldn't be required to include this jar as an explicit dependency at all...
    – fateddy
    Commented Jul 11, 2015 at 5:17
  • I know... But heritrix (web crawler) uses this. So this problem prevents me from compiling heritrix...
    – old_bear
    Commented Jul 11, 2015 at 5:29
  • 2
    Can you supply a link to the Maven wiki page you are referring to. Commented Jul 11, 2015 at 8:38
  • 2
    maven system dependencies
    – old_bear
    Commented Jul 14, 2015 at 9:28

3 Answers 3

26

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.

5
  • 2
    How to specify same in gradle? Any idea?
    – sakura
    Commented Feb 8, 2016 at 9:15
  • 1
    Hat off you man, you saved me i was looking for this since 5 hours thanks :)
    – KhAn SaAb
    Commented Aug 29, 2018 at 13:39
  • I'm trying to build Openfire using Intellij IDEA. I have latest Java SDK 1.8 installed on my Win 10 machine. Got the same error on import sun.security.x509.GeneralNameInterface;. There are multiple pom.xml files, one global for a project folder, one in xmppserver subfolder. I tried to add the settings you described - it didn't fix error.
    – vkelman
    Commented Feb 7, 2019 at 16:56
  • @vkelman Due to the multiple POMs: Did you try mvn help:effective-pom on the project in question to see whether the setting is really applied to it. Commented Feb 7, 2019 at 17:28
  • 1
    Thanks ;) Exact solution.
    – pujan kc
    Commented Jul 29, 2021 at 19:13
2

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! ;)

0

I solved it like this:

  1. Add to JAVA_HOME/lib/rt.jar
  2. Go to Eclipse Menu Windows -> Windows -> Preferences -> Java -> Installed JREs
  3. Select jdk version. In me case jdk-11.0.4
  4. Select Add External JARs.. and add JAVA_HOME/lib/rt.jar
1
  • 1. With powerful build tools available nowadays copying dependencies around manually isn't state of the art. 2. Isn't it enough to refer to .../jdk/jre/lib/rt.jar in Eclipse? Commented Nov 13, 2020 at 17:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.