Sonntag, 24. März 2013

Trying out Nashorn in Eclipse


Setup


Download Java 8 EAP b82 or greater from
http://jdk8.java.net/download.html
or go to
http://java.net/projects/adoptopenjdk/pages/AdoptOpenJDK#Starter_Level
to build Java 8 from source.

Go to
https://github.com/AdoptOpenJDK/NashornHackDay
and clone the Respository (or download a zip). The directory should be named 'NashornHackDay'

Download the Java Developers edition of eclipse if you don't have it installed yet:
http://www.eclipse.org/downloads

Create an Eclipse Project

  • Run eclipse and choose a workspace
  • You might want to install the 'Javascript Development Tools': Help->Install New Software: Select Juno Update Site, check 'Group Items by Category', Scroll to 'Web,XML,..' and select the plugin
  • Create a new Java Project Nashorn

Java calling Javascript

  • Copy TestInterop.java from NashornHackDay/java-interop/simple to the src folder of your eclipse project
  • Copy script.js to the main folder of your Nashorn project
  • Go to Menu: Window->Prefernces->Java->Installed JREs
  • Add the Java 8 JDK to the installed JREs by choosing the main folder of the JDK
  • Go back to your project
  • Open the Context-Menu of the Project and go to Properties
  • Under Java-Build-Path remove the JRE Library on the Libraries Tab
  • Add the Java 8 JDK on the same TAB via Add Library->JRE System Libraries
  • Exit the Dialog
  • Try to run the TestInterop.java now.

Javascript calling Java

  • Add a new folder called script in the project Nashorn main-folder in eclipse
  • Copy the *.js scripts from the NashornHackDay/examples folder into this location
  • Create a new script nashorn.sh in the NashornHackDay directory
  • Put this into the file
 #!/bin/sh

 $JAVA8_HOME/bin/jjs <path_to>/NashornHackDay/lib/hackday/bootstrap.js $1
 

  • Where $JAVA8_HOME should point to your OpenJDK 8 build/installation.
  • Change the <path_to> to the path of your NashornHackDay directory
  • Go back to eclipse and click on the small down arrow on the button right to the Run-Button in the Toolbar
  • Open the menu entry "External Tools Configurations"
  • Click on Program in the left navigation part and open the Context Menu: New
  • Put in the Following:
Name: RunNashorn
Location: <path_to>/NashornHackday/nashorn.sh
Working Directory: ${workspace_loc:/Nashorn}
Arguments: ${selected_resource_loc}

  • Go to tab: Refresh and select 'Refresh resources upon completion' and 'The project containing the selected resource'
  • Go to tab: Build and unselect 'Build before launch'
  • Go to tab Common and select 'Display in favorites menu: External Tools'
  • 'Close' the Dialog
  • Choose a *.js file from your script-directory and open the 'External Tools' chooser from the Toolbar again and choose 'RunNashorn'
  • Eclipse will run the *.js File with Nashorn now and show the results in the Console in eclipse

JavaScript Help

If you downloaded the nashorn source as described on the GitHub page or have the source of the OpenJDK then go to the docs directory to find a
Java Scripting Programmer's Guide (JavaScriptingProgrammersGuide.html).