Java 8 is a major release for the java programming language and java virtual machine. It includes many changes in that we are going to discuss about nashorn javascript engine example. Java se 8 provided a brand new library JavaScript engine named nashorn. Nashorn is completely new engine. It can used from command line or directly in your java code. This is entirely written from scratch. Let you write JavaScript code using java classes and objects. 

How to use Nashorn from command line

If you are working on windows first open the command prompt type jjs now we can see the jjs prompt appear and now you can start typing javascript code.

Nashorn from command line

If you want to read some content from remote server first copy the url from the browser and copy into the java.net.URL class. Please check the below code

java 8 nashorn engine example

var input = new 
java.util.Scanner(new java.net.URL
(“https://googledrive.com/host/0Bw-ZBbYmW_PRWkFJRDJYczUzYTg”).openStream())
input.useDelimiter(‘$’)
var data = input.next()
data
https://www.youtube.com/watch?v=FNNuDoU6m8U

How to write JavaScript in separate file and then execute that file from java code.

We can also build javascript into your java code you can write complete code blocks using javascript and execute easily. ScriptEngineManager class is used the get the script engine object. It has access to all script engine that are build into java runtime.

package com.example.java8;

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

public class java8NashornExample {

public static void main(String[] args) {

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("nashorn");
String script =  "var message = 'Welcome to StudywithDemo'; "
+ "message";
String result;
try {
result = (String)engine.eval(script);
System.out.println(result);
} catch (ScriptException e) {
System.out.println("There was a JavaScript error"+e.getMessage());
e.printStackTrace();
}
}
}

When you running complex javascript in java code it is better to put javascript in separate file. Look the below example.

package com.example.java8;

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

import java.io.*;

public class java8NashornExample2 {

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

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("nashorn");
File f = new File("jsfolder/sample.js");
Reader read = new FileReader(f);
String result;
try {
result = (String)engine.eval(read);
System.out.println(result);
} catch (ScriptException e) {
System.out.println("There was a JavaScript error"+e.getMessage());
e.printStackTrace();
}
}
}

sample.js

var feed = 'https://googledrive.com/host/0Bw-ZBbYmW_PRWkFJRDJYczUzYTg';
var url = new java.net.URL(feed);
input = new java.util.Scanner(url.openStream());
input.useDelimiter('$')
var data = input.next()
data

1 comments:

  1. :d
    It is nice article to improve my knowledge.thank you for sharing useful post
    visit
    web programming tutorial
    welookups

    ReplyDelete

 
Top