Friday, July 7, 2017

I want my Rhino back! Switching to Nashornless.

In JDK 8 default scripting engine had been changed. Nashorn replaced Rhino. I understand all positives of this change and how good the use of InvokeDynamic is, etc.
But this change may be also seen as a breaking change. Which is quite normal in world of JavaScript btw :D And do not expect compile time errors! It is almost like node experience...

Ok, your old Rhino-specific code will not run with Nashorn. However, I have been using it for years, and truly speaking missing it a lot. I wrote test tooling scripted by Rhino, glue code that connects ETL to Java or dynamic backend of application, allowing website maintenance with no redeployment. Still, it is possible to go back to old good JavaScript implementation. So let's make our Rhino great again!

We could, of course, use it like we did it before JDK 6: add just a lib to our project and evaluate script by means delivered by specific Mozilla implementation.

If we are more familiar with Java scripting in JSR-223 style we would need extra code that is Sun specific part, already removed from recent Java. But fortunately someone already did the job for us and provided alternative JSR-223 wrapper that uses original Rhino and provides all extra classes that are required by standardized engines.
Let's just add dependency to pom.xml file:

And yes, now we can use old, good, nashornless Rhino again! Just one more thing to remember is to use right scripting engine instead of "javascript".

new ScriptEngineManager().getEngineByName("rhino");