Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nashorn under Java 7

I'm trying to get Oracle's Nashorn running under Java 7.

I've cloned the repo and compiled the source. All I had to do to get it to compile was:

  • Change jdk.internal.org.objectweb.asm.* to org.objectweb.asm.*
  • Change one line that had Long.hashCode() static.

Everything seems happy, and Java 7 sees Nashorn as a scripting engine; but when I try and get an instance:

NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine();

I get the exception:

java.lang.RuntimeException: java.lang.ClassNotFoundException: jdk.nashorn.internal.objects.Global at jdk.nashorn.internal.runtime.Context.newGlobalTrusted(Context.java:831) at jdk.nashorn.internal.runtime.Context.newGlobal(Context.java:636) at jdk.nashorn.api.scripting.NashornScriptEngine$2.run(NashornScriptEngine.java:270) at jdk.nashorn.api.scripting.NashornScriptEngine$2.run(NashornScriptEngine.java:266) at java.security.AccessController.doPrivileged(Native Method) at jdk.nashorn.api.scripting.NashornScriptEngine.createNashornGlobal(NashornScriptEngine.java:266) at jdk.nashorn.api.scripting.NashornScriptEngine.(NashornScriptEngine.java:104) at jdk.nashorn.api.scripting.NashornScriptEngine.(NashornScriptEngine.java:78) at jdk.nashorn.api.scripting.NashornScriptEngineFactory.getScriptEngine(NashornScriptEngineFactory.java:139)

jdk.nashorn.internal.objects.Global is in my class path. Does Nashorn use a different class loader maybe?

like image 502
Kong Avatar asked Jun 05 '13 01:06

Kong


People also ask

When was Nashorn removed Java?

With the release of Java 11, Nashorn was deprecated citing challenges to maintenance, and has been removed from JDK 15 onwards.

Which is replaced with 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?

JAXenter: The motivation behind the proposal to deprecate Nashorn was that it is “challenging to maintain”.

What is Nashorn in Java 8?

Nashorn: Nashorn is a JavaScript engine which is introduced in JDK 8. With the help of Nashorn, we can execute JavaScript code at Java Virtual Machine. Nashorn is introduced in JDK 8 to replace existing JavaScript engine i.e. Rhino.


1 Answers

I tried getting it to work under 7 previously and was met by the same error as you have. I think it has to do with the fact that Nashorn makes use of some new features in Java 8.

There is a backport of Nashorn for JDK 7 on bitbucket; I wrote a blog post detailing how to use it on my works blog here. You can also look at this stack overflow question for more information.

That bitbucket project hasn't been updated in some time, I'd recommend that you use the preview of Java 8 if you want to play with Nashorn.

like image 166
Lyndon Armitage Avatar answered Sep 23 '22 10:09

Lyndon Armitage