1

The class in question is sun.security.tools.KeyTool and I'm using OpenJDK 7. I keep getting "cannot find symbol" though, and I'm no Java expert so I can't figure out through documentation why that could be or if it's gone. Any ideas?

Exact error message:

Main.java:1: error: cannot find symbol
import sun.security.tools.KeyTool;
                         ^
  symbol:   class KeyTool
  location: package sun.security.tools

Code:

import sun.security.tools.KeyTool;

public class Main {
    public static void main(String[] args) {
    }
}
Yousef Amar
  • 651
  • 6
  • 19
  • 1
    What's your code? What have you tried? What's the exact error message, and when do you get it? – AntonH Feb 27 '17 at 16:25
  • @AntonH I get it on compilation. javac version 1.7.0_131. My code is `import sun.security.tools.KeyTool;` and an empty class. – Yousef Amar Feb 27 '17 at 16:27
  • @AntonH I added more detail to the question if it wasn't clear. – Yousef Amar Feb 27 '17 at 16:33
  • @c0der Add an external jar for `sun.*`? Are you sure? To clarify, `import sun.security.tools.*;` works just fine — it's only `KeyTool` that seems to be missing. – Yousef Amar Feb 27 '17 at 16:34
  • Use `import sun.security.tools.keytool.*;` – c0der Feb 27 '17 at 16:51
  • @c0der Thanks for the suggestion, but in that case I get `error: package sun.security.tools.keytool does not exist` (even with different capitalization). – Yousef Amar Feb 27 '17 at 16:56
  • 1
    I know you said your `import sun.security.tools.*` works but since I'm not really sure what else your importing you can give this a quick look http://docs.oracle.com/javase/7/docs/technotes/guides/security/p11guide.html. Make sure your 2 security libraries live in `$JAVA_HOME/lib/security` – jiveturkey Feb 27 '17 at 16:57

1 Answers1

2

Try using javac -XDignore.symbol.file ....

By default javac restricts the classes it exposes to users. This generally helps users avoid accidentally depending on (unsupported) classes that are not public Java API but happen to be available in the current JRE/JDK. The list of "safe" classes is described in ct.sym file. The ignorel.symbol.file system property tells javac to ignore that file and make use of all classes available in the JDK/JRE.

And make sure you add tools.jar, where this class is defined to the classpath for javac.

omajid
  • 14,165
  • 4
  • 47
  • 64
  • Do you know why it's necessary to add tools.jar? When I use Java 8, everything works fine without the extra classpath, although I do have to use `-XDignore.symbol.file`. Do you know why that flag is needed too? – Yousef Amar Feb 27 '17 at 18:03
  • I added an explanation of `-XDignore.symbol.file.` As for Oracle Java 8 vs OpenJDK 7, I am not sure. Compare OpenJDK 8 to see what it does. Set the `_JAVA_LAUNCHER_DEBUG` environment variable to see what classes are added to the classpath by the launcher. – omajid Feb 27 '17 at 18:29
  • A reminder note for future readers: in Java 8, the keytool (and jarsigner) code moved into their own packages, so the `sun.security.tools.KeyTool` class became `sun.security.tools.keytool.Main`. The public access function is still `main()`. – Ti Strga May 16 '18 at 19:38