user6289110
user6289110

Reputation: 1

Use Nashorn but not javax.script

I have a project to complete and I can't use javax.script, so can I use Nashorn but not use javax.script?

Upvotes: 0

Views: 142

Answers (2)

Attila Szegedi
Attila Szegedi

Reputation: 4565

You can use Nashorn API directly, e.g. you can start from the jdk.nashorn.api.scripting.NashornScriptEngineFactory class, but a lot of the API implicitly relies on the javax.script package later on. I'm not sure why you have the limitation, though; javax.script is a standard JDK package, it's in every Java runtime since around Java 1.5 or 6.

That said, direct API usage allows you to get customized ScriptEngines, because NashornScriptEngineFactory has some overloads for the getScriptEngine() method that allow you to pass jjs command line arguments, a custom class loader to use by the engine, and so on.

Another minor thing that using Nashorn API directly buys you is that you will know that you're definitely getting a Nashorn engine, something you couldn't necessarily be certain if you just asked for a JS engine from javax.script.

Upvotes: 1

A. Sundararajan
A. Sundararajan

Reputation: 4405

You can use "jjs" command line tool in jdk/bin to evaluate scripts. That tool can evaluate scripts from files/urls or can also work in interactive mode. The tool does not use javax.script API - but uses Nashorn directly

Upvotes: 1

Related Questions