Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 254
Germano Carella Avatar asked Oct 28 '16 21:10

Germano Carella


People also ask

What is sun package in Java?

Subpackage in java Package inside the package is called the subpackage. It should be created to categorize the package further. Let's take an example, Sun Microsystem has definded a package named java that contains many classes like System, String, Reader, Writer, Socket etc.

What is sun class in Java?

The classes in sun. * are present in the JDK to support Oracle's implementation of the Java platform: the sun. * classes are what make the Java platform classes work "under the covers" for Oracle's JDK. These classes will not in general be present on another vendor's Java platform.


1 Answers

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.

like image 159
dbalakirev Avatar answered Oct 13 '22 10:10

dbalakirev