Nashorn for Java 7

EDIT: I’m not sure if this is the right category, feel free to move it if you want.

The problem

So, since Sponge came, i had a lot of ideas for plugins. One of them is a plugin for scripting in Javascript, along with some Node-like features.

To program this plugin, i needed a decent Javascript engine. I had two choices:

  1. Use Javas builtin Javascript engine (via JSR-XXX), based on Mozilla Rhino, or
    2 Use the ‘standalone’ Rhino, which has a bit better performance than the builtin one.

So, it was a logical decision to go for the standalone Rhino. However, the Rhino project was started in 1997, and lacks a lot of performance.

Recently, Oracle released a wonderful project, called Nashorn. Nashorn is a very fast Javascript engine, included in Java 8. For people who want to use Javascript in their Java program, this is a good option.

However, since Minecraft and Sponge, and most Java users nowadays, are not running on Java 8, but on Java 7, or even Java 6, it is not an option to use Nashorn.


The solution

After some research, i found a Nashorn backport (by ramonza) to Java 7. This was excellent! I forked the project, and i am running it on GitHub. You can easily build it using Gradle and add it to your project as an external library, or if you’re using Git, you can add the project as a submodule. And voilá, you are running Nashorn on Java 7.

I made some changes/additions to Nashorn:

  • Switch from Ant to Gradle (for easier building)

Since there are some people interested in a Javascript engine, i decided to publish and share it. The repo can be found here.

A more extensive tutorial is available here.

The project is by far not finished, so feel free to contribute, look at it, throw potatoes at it if you feel like, or test it.

BTW:
The only downside i can find on Nashorn is that the startup is very slow. However, it’s blazingly fast after a warming-up.

4 Likes

In my own uses, I’ve found Nashorn to work well. I’m currently implementing scripting into a personal game engine I’m writing (mostly to avoid writing boilerplate code twice in similar games) and I’ve played with it. Very nice, and especially since JavaScript is the top language on Github [citation needed] and it would make plugin development available to a lot more younger gamers.

I’ll possibly contribute to your code if it looks like you need help, since I think JS is a great way to quickly create plugins.

3 Likes

Updated the repository: https://github.com/exstar/nashorn. I removed the builtin module loading for now, but it may come back as an extent.

Mojang is currently working on a new launcher that packages java 8 with minecraft, so this shouldn’t be as much of a problem in the future.

wait what?

You should find this helpful.

2 Likes