Zo72 Zo72 -4 years ago 164
Java Question

Java: Is it safe to import from jdk.*?

Is it safe for me to write a Java program with imports like this:

import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import jdk.nashorn.api.scripting.ClassFilter;

In my example I import Nashorn, but it could be any other jdk package.

Note: I know that it's safe/correct to import from java.* , javax.* and unsafe/unsupported to import from sun.*

I saw a good article here: http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html

I can't find anywhere in the Java official documentation that tells me one way or another


Answer Source

You are supposed to use them through javax.script as explained on https://docs.oracle.com/javase/8/docs/jdk/api/nashorn/jdk/nashorn/api/scripting/package-summary.html

Package jdk.nashorn.api.scripting Description

This package provides the javax.script integration, which is the preferred way to use Nashorn. You will ordinarily do this to obtain an instance of a Nashorn script engine:

import javax.script.*;
ScriptEngine nashornEngine = new ScriptEngineManager().getEngineByName("Nashorn");

But to answer your question, i would say "Probably not". The only officially guaranteed supported packages are java.*,javax.*, and org.*.

Any new unsupported packages would not be named sun.* since Oracle now owns Java.

It's likely that someone picked jdk.* as a new unsupported package prefix since sun.* is no longer available.

That said, it's somewhat unlikely that they will remove it any time soon, unless they decide to replace it for another Javascript engine, like they already did before.

The other potential problem is that your program won't run on another JDK that does not include Nashorn.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download