Search code examples
javaimportjava-9java-packagejshell

Why can't I import sun packages?


I downloaded java 9 from https://jdk9.java.net and installed it on my Windows 10 machine.
Java 9 has jshell, which I can use to evaluate and learn java programming.
Some examples import sun packages, such as sun.audio.* etc. But, in jshell, every time I try to import any sun package, it says the sun package does not exist.

Some applications do not work with java 9. Perhaps there are incompatibilities?


Solution

  • It seems that this is related to how Java 9 tries to support encapsulation for real, in contrast to how it was done in the past. It could have happened that your internal classes were exposed to consumers, because your the public API used those internal classes. Sun internal packages had a similar issue.

    There are three topics in the modularity story for Java 9:

    • Modularize the JDK and JRE
    • Hide platform internals (e.g. sun.misc)
    • A module system for application developers

    http://paulbakker.io/java/java-9-modularity/

    For your specific case, the sun internal modules are not available for import by default and that is a good thing.

    That said, modules can be added to JShell thanks to a fix.

    There should be "add modules" support as well as "add exports" and "module path", to allow people to explore their own modular code. https://bugs.openjdk.java.net/browse/JDK-8166649

    You want to track that bug ticket to see when this fix is available. It was just resolved 4 days ago.