Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start java nashorn with jjs arguments

I would like to read a bytecode generated by nashorn engine. I have found that argument i need is -d=*folder* also i would like to apply optimistic types for better performace which are enabled by argument-ot

Im initializing the engine by calling methods:

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
engine.eval(myscriptfile);

But I have not found where am i supposed to put the jjs arguments.

like image 888
user2749903 Avatar asked Sep 13 '15 12:09

user2749903


People also ask

What is JJS command?

If you wonder what jjs stands for, it stands for Java JavaScript. The command is located in the JDK_HOME\bin directory. The command can be used to run scripts in files or scripts entered on the command-line in interactive mode. It can also be used to execute shell scripts.

What is replacing Nashorn?

GraalVM can step in as a replacement for JavaScript code previously executed on the Nashorn engine. GraalVM provides all the features for JavaScript previously provided by Nashorn. Many are available by default, some are behind flags, and others require minor modifications to your source code.

Why is Nashorn deprecated?

With the release of Java 11, Nashorn was deprecated citing challenges to maintenance, and has been removed from JDK 15 onwards. Nashorn development continues on GitHub as a standalone OpenJDK project and the separate release can be used in Java project from Java 11 and up.


2 Answers

The javax.script API doesn't let you pass these arguments. You'll need to use the explicit Nashorn API to get a script engine factory:

import jdk.nashorn.api.scripting.NashornScriptEngineFactory;

NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine("--optimistic-types=true", "-d=someFolder");

Hope that helps.

like image 156
Attila Szegedi Avatar answered Oct 14 '22 13:10

Attila Szegedi


Adding to what Attila said: you can set "nashorn.args" System property with the arguments you want to pass to Nashorn.

Pro: You can stick to javax.script API in your code and still pass arguments.

Con: This affects all the nashorn engines created in the process - whereas nashorn specific API allows you create different engine instances with different command line arguments. Also, you may not have control over System property setting in certain deployments.

like image 1
A. Sundararajan Avatar answered Oct 14 '22 15:10

A. Sundararajan