最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Enable scripting mode for nashorn in java - Stack Overflow

programmeradmin3浏览0评论

I have to execute some bash shell mands from Java using nashorn.

I have a javascript file:

#!/usr/bin/jjs

var testBashMethod = function(name){

    $EXEC("echo Hello from bash ${name}");
};

testBashMethod("foobar");

I have java method loading the above javascript method into Nashorn engine and executing it:

public void executeScript(){

    ScriptEngineManager engineManager = new ScriptEngineManager();
    ScriptEngine engine = engineManager.getEngineByName("nashorn");

    engine.eval(new FileReader("script.js"));

    Invocable invocable = (Invocable)engine;
    invocable.invokeFunction("testBashMethod");
}

On executing the above method I get the following error:

jdk.nashorn.internal.runtime.ECMAException: ReferenceError: "$EXEC" is not defined

My guess is that I need to load the nashorn engine in scripting mode in java. On the terminal I can run the engine with scripting mode then the following executes successfully:

jjs -scripting
jjs> $EXEC('echo Hello World..!!')

My Question: How do I load the nashorn engine in Java in scripting mode? so that bash scripting methods are available. Or is there something else that I am missing.

Thank you for the help.

I have to execute some bash shell mands from Java using nashorn.

I have a javascript file:

#!/usr/bin/jjs

var testBashMethod = function(name){

    $EXEC("echo Hello from bash ${name}");
};

testBashMethod("foobar");

I have java method loading the above javascript method into Nashorn engine and executing it:

public void executeScript(){

    ScriptEngineManager engineManager = new ScriptEngineManager();
    ScriptEngine engine = engineManager.getEngineByName("nashorn");

    engine.eval(new FileReader("script.js"));

    Invocable invocable = (Invocable)engine;
    invocable.invokeFunction("testBashMethod");
}

On executing the above method I get the following error:

jdk.nashorn.internal.runtime.ECMAException: ReferenceError: "$EXEC" is not defined

My guess is that I need to load the nashorn engine in scripting mode in java. On the terminal I can run the engine with scripting mode then the following executes successfully:

jjs -scripting
jjs> $EXEC('echo Hello World..!!')

My Question: How do I load the nashorn engine in Java in scripting mode? so that bash scripting methods are available. Or is there something else that I am missing.

Thank you for the help.

Share Improve this question asked Nov 11, 2014 at 19:45 PanshulPanshul 1,0822 gold badges14 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine engine = factory.getEngine(new String[] { "-scripting" });

See the Nashorn Wiki for lots more documentation. I got (and modified) the above snippet from the Nashorn jsr223 engine notes page.

You can also define nashorn options via via "nashorn.args" System property as well. So, something like

java -Dnashorn.args=-scripting MyMainClass

will work and your code can stick to javax.script API (need not use jdk.nashorn.api.scripting API). But this implies all engines created by your Java process will have scripting mode enabled.

发布评论

评论列表(0)

  1. 暂无评论