Java 1.8 + JavaScript Engine Nashorn + Debugging javascript…
Introduction
This is an extremely quick tutorial about using javascript engine (nashorn) usage in Java 1.8
These are the versions of apis and frameworks etc. used;
- Java 1.8
- JUnit 4.11
- IntelliJ IDEA 13.1.4
WARNING: Using java versions below 8 will cause java.lang.NullPointerException as nashorn javascript engine comes with java 8. |
Within this tutorial you will see
- Testing (Kind of encouraging test driven development)
- Creating and using nashorn javascirpt engine
- Calling javascript functions from java
- Calling java methods from javascript
- Debugging javascript file using IntelliJ IDEA
Project Structure
INFO: Using maven is not really necessary for this project as all, infact JUnit is the only dependency, nashorn comes with jdk bundle. It is besically just an encouragement. |
Dependencies
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>info.hevi.learn</groupId> <artifactId>javascriptengine</artifactId> <version>1.0</version> <description>simple javascript engine tutorial with debugging</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.11</junit.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </project>
Code
Test File(MainTest.java)
public class MainTest { public String getName(){ return "hevi.info"; } @Test public void testScript() { ScriptEngineManager engineManager = new ScriptEngineManager(); ScriptEngine scriptEngine = engineManager.getEngineByName("nashorn"); String fileName = "src/main/resources/jsfile.js"; String functionName = "doIt"; try { scriptEngine.eval("load('" + fileName + "');"); Invocable inv = (Invocable) scriptEngine; String retValue = (String) inv.invokeFunction(functionName, new MainTest()); System.out.println(fileName + "@" + functionName + " returned " + retValue); } catch (ScriptException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }
JavaScript File(jsfile.js)
function doIt(param) { var message = "hello world " + param.getName() + "!"; return message; }
So what happenes above is that doIt javascript function is called fully parametric, and pass MainTest instance as a parameter to the method;
inv.invokeFunction(functionName, new MainTest())
The doIt function calles getName() method which corresponds to a java method defined in MainTest class.
Debugging
Debugging is as just easy as debugging java files using IntelliJ IDEA 13.1. Here you can find here the IntelliJ blog for the debugging feature. The only point is that you suppose to use “nashorn” engine instead of raw “javascript” for creating scprint engine as below;
ScriptEngine scriptEngine = engineManager.getEngineByName("nashorn");
Unfortunatelly java 8 is the requirement here.
Download Source Code
Download project – javajavascriptengine.zip |