Search code examples
javanashorn

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; 

Note: 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

thanks


Solution

  • 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.*.

    Since those are the only supported packages, it follows that all other packages are not supported.

    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.