2

Anyone got any pointers on how to start playing with Oracle's new JS engine, Nashorn?

I've installed JDK 8 b80 and still only Rhino:

@Test
public void list() {
   ScriptEngineManager manager = new ScriptEngineManager();
   List<ScriptEngineFactory> factories = manager.getEngineFactories();
   for (ScriptEngineFactory factory : factories) {
       System.out.println(factory.getEngineName());
   }
}

Output:

"C:\Program Files\Java\jdk1.8.0\bin\java" -ea -Didea.launcher.port=7537...
Mozilla Rhino
Kong
  • 8,792
  • 15
  • 68
  • 98
  • 2
    nashorn-jdk8 is a separate branch as of March 2013. You can check it out and build nashorn.jar separately. I updated my answer [here](http://stackoverflow.com/a/13430909/993133) – pd40 Mar 10 '13 at 12:28

2 Answers2

1

Install the JDK8 and create an alias for your JDK's jjs (Nashorn Interpreter), e.g., if you create a file called test.js, you can run the program with:

$ jjs test.js

Mac OS = alias jjs=’/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/bin/jjs’

Windows = Define an environment variable called ‘JAVA8_HOME’ and point to your jdk8 folder, then you can invoke jjs by running this command:

“%JAVA8_HOME%\jre\bin\jjs” test.js

Here's an example of a Nashorn App: http://marcelorjava.wordpress.com/2013/08/24/code-walkthrough-online-kanban-board-with-nashorn/

the_marcelo_r
  • 1,847
  • 22
  • 35
  • 2
    On a Mac, you don't need to (and probably shouldn't) hardcode the path to jdk8. Use: `alias jjs='$(/usr/libexec/java_home -v 1.8)/jre/bin/jjs'` – hohonuuli Feb 26 '14 at 22:09
  • 2
    I like (cd /usr/bin/ ; ln -s $JAVA_HOME/bin/jjs jjs) myself. That way I can use jjs in shebang scripts. – wickund May 12 '14 at 11:22
1

Sorry, I don't like the alias-thing either, so as wickund suggested I'll put it short (and also use the trick hohonuuli posted)

cd /usr/bin; ln -s $(/usr/libexec/java_home -v 1.8)/bin/jjs jjs

Do it as root, else you won't be lucky because of missing permissions

Chris
  • 1,119
  • 1
  • 8
  • 26