1

Here's my full code:

import javax.script.*;

public class TestF {
    public static void main(String[] args) throws Exception {
        // create a script engine manager
        ScriptEngineManager factory = new ScriptEngineManager();
        // create a Nashorn script engine
        ScriptEngine engine = factory.getEngineByName("nashorn");
        // evaluate JavaScript statement
        try {
            engine.eval("print('Hello, World!');");
        } catch (final ScriptException se) {
            se.printStackTrace();
        }
    }
}

I'm using Arch and just downloaded Java and have the Java extension on VSCode. I can run any other programs in Java but have not installed any external modules/packages. Running the above program gives me

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "javax.script.ScriptEngine.eval(String)" because "<local2>" is null

It seems like factory.getEngineByName("nashorn") is returning null. Do I need to externally download Nashorn? Because this says it's already included automatically. Or is nashorn not supported anymore? Because this says I can still use it. I'm using the latest version of Java18 from their official website.

DavidNyan10
  • 100
  • 8
  • Newer releases of the JDK do not include JavaScript support, and JDK18 is definitely newer. – Pointy May 08 '22 at 14:31
  • [Relevant similar questions](https://www.google.com/search?q=site%3Astackoverflow.com+java+nashorn+null) – Hovercraft Full Of Eels May 08 '22 at 14:32
  • From [the Java 15 release notes](https://www.oracle.com/java/technologies/javase/15-relnote-issues.html#JDK-8236933): “The Nashorn JavaScript script engine, its APIs, and the jjs tool have been removed. The engine, the APIs, and the tool were deprecated for removal in Java 11 with the express intent to remove them in a future release.” You won’t get a more authoritative answer than that. – VGR May 08 '22 at 16:36
  • @VGR What about Rhino? Is it still usable? I tried with Rhino and got the exact same result as in my question. – DavidNyan10 May 12 '22 at 03:16
  • I believe Nashorn originally replaced Rhino. Currently, JDKs do not come with any JavaScript engine at all, though you can always include one in the classpath or module path. – VGR May 12 '22 at 03:37
  • @VGR Oh, so how can I do that? Sorry, I'm getting off-topic, I'm new to Java. I first asked on SO how to use JavaScript engines in Java18 and they said to use Nashorn or Rhino, closing my question and lowering my reputation :(. But when I did try using them, I get the above error. The question marked as duplicate said Nashorn and Rhino got depreciated. So, I asked on SO again, how to run js in java when they're depreciated but they closed the question saying "what makes you think they're depreciated?", sending me back here to asking this question. – DavidNyan10 May 15 '22 at 10:22
  • The classpath is a list of places Java will look for classes. Normally it’s a list of .jar files, separated by `:` (or `;` in Windows). Directories containing package directories are also valid entries. If you’re running on the command line, you set it with something like `java -cp /path/to/myprogram/classes:/path/to/nashorn.jar mypackage.MyProgram`. if running from an IDE, you usually let you set it with a project setting that allows you to add “libraries.” – VGR May 16 '22 at 11:44

0 Answers0