Using Javascript with Java8 Nashorn (Part I)

This article shows how to use Javascript with Java8 Nashorn engine

1. Using Javascript print(‘…’); function as a String in Java

Example1

package com.dzone.java8nashorn;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Example1 {
    
    public static void main(String[]args) throws Throwable {
        ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("nashorn");
        scriptEngine.eval("print('Hello world ! Welcome to Dzone');");
    }
}

Output


Welcome to DZone

2. Using Javascript code as a File in Java

dzone.js

var welcomeMessage = function(platformName) {
    print('Welcome to ' + platformName);
    return "Received in Java code from Javascript";
};
Example2.java

package com.dzone.java8nashorn;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;

public class Example2 {
    private static final String name ="DZone";

    public static void main(String[]args) throws Throwable {

        ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("nashorn");
        scriptEngine.eval(new FileReader("js/dzone.js"));

        Invocable invocable = (Invocable) scriptEngine;
        Object message = invocable.invokeFunction("welcomeMessage",name);
        System.out.println(message);
    }
}

Output


Welcome to DZone
Received in Java code from Javascript

Note : If we are to write invocable.invokeFunction("welcomeMessage"); i.e without including the argument

Output


Welcome to undefined
Received in Java from Javascript

3. Using Javascript code to identify Java object

object.js

var getJavaClassObject = function (objectName) {
    print("Java Object class is : " + Object.prototype.toString.call(objectName));
    return "Output inside Java using Javascript code"
};
Example3.java

package com.dzone.java8nashorn;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;

public class Example3 {
    public static void main(String[]args) throws Throwable {

        ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("nashorn");
        scriptEngine.eval(new FileReader("js/object.js"));

        Invocable invocable = (Invocable) scriptEngine;
        System.out.println(invocable.invokeFunction("getJavaClassObject", new Example2()));
    }
}

Output


Java Object class is : [object com.dzone.java8nashorn.Example2]
Output inside Java using Javascript code

4. Using Javascript function to encode and decode a URI with Java

encode.js

var encode = function(url) {
    return encodeURI(url)
};
decode.js

var decode = function(url) {
    return decodeURI(url)
};
Example4.java

package com.dzone.java8nashorn;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;

public class Example4 {
    private static final String url="https://dzone.com?ådmin ¬login£&";
    
    public static void main(String[]args) throws Throwable {

        ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("nashorn");
        scriptEngine.eval(new FileReader("js/encode.js"));
        scriptEngine.eval(new FileReader("js/decode.js"));

        Invocable invocable = (Invocable) scriptEngine;
        System.out.println(invocable.invokeFunction("encode", url ));
        System.out.println(invocable.invokeFunction("decode",url));
    }
}

Output


https://dzone.com?%C3%A5dmin%20%C2%AClogin%C2%A3&
https://dzone.com?ådmin ¬login£&

I hope you have seen how to use Javascript with Java8 Nashorn engine

References

  1. Oracle Nashorn: A Next-Generation JavaScript Engine for the JVM
  2. Java 8 Nashorn Tutorial

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: