Nashorn

Die JavaScript-Engine "Nashorn" ist eine Leistungsstarke und standardkonforme JavaScript-Engine (ECMAScript Edition 6). Nashorn ist zu 100 Prozent in Java geschrieben. Nashorn steht im JDK 8 direkt zur Verfügung und ersetzt die bisherige Referenz-Implementierung von Mozilla mit dem Namen Rhino.

class HelloNashorn


package de.snowbits.js;

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

public class HelloNashorn {

	public static void main(String[] args)  {
		try {
			ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
			engine.eval("print('Hello Nashorn');");
		} catch (ScriptException e) {
			e.printStackTrace();
		}
	}

}
	

class NashornFile


package de.snowbits.js;

import java.io.FileNotFoundException;
import java.io.FileReader;

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

public class NashornFile {
	
	public static void main(String[] args)  {
		try {
			ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
			Invocable invocable = (Invocable) engine;
			engine.eval(new FileReader(NashornFile.class.getResource("script.js").getPath()));

			Object result = invocable.invokeFunction("sayHello", "Wolfgang Garbe");
			System.out.println(result);
			System.out.println("Object: " + result.getClass());
			
			// Hallo Wolfgang Garbe, Beste Grüße aus Java
			// Hallo Wolfgang Garbe
			// Beste Grüße vom JavaScript
			// Object: class java.lang.String
		} catch (ScriptException | NoSuchMethodException | FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static String sayHello(String name) {
	    System.out.format("Hallo %s", name);
	    return ", Beste Grüße aus Java";
	}

}
	

JavaScript script.js


var MyJavaClass = Java.type('de.snowbits.js.NashornFile');
var result = MyJavaClass.sayHello('Wolfgang Garbe');
print(result);

var sayHello = function(name) {
    print('Hallo ' + name);
    return "Beste Grüße vom JavaScript";
};
	

class NashornPerson


package de.snowbits.js;

import java.io.FileNotFoundException;
import java.io.FileReader;

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

import jdk.nashorn.api.scripting.ScriptObjectMirror;

public class NashornPerson {
	
	public static void main(String[] args)  {
		try {
			ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
			engine.eval(new FileReader(NashornPerson.class.getResource("person.js").getPath()));
			
			// Name: Wolgang Garbe
		} catch (ScriptException | FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static void loadFullName(ScriptObjectMirror person) {
	    System.out.println("Name: " + person.callMember("getFullName"));
	}

}
	

JavaScript person.js


var MyJavaClass = Java.type('de.snowbits.js.NashornPerson');
MyJavaClass.loadFullName(new Person("Wolgang", "Garbe"));

function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.getFullName = function() {
        return this.firstName + " " + this.lastName;
    }
}
	

class NashornArray


package de.snowbits.js;

import java.io.FileNotFoundException;
import java.io.FileReader;

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

public class NashornArray {
	
	public static void main(String[] args)  {
		try {
			ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
			engine.eval(new FileReader(NashornArray.class.getResource("array.js").getPath()));
			
			// Apfel
			// Birne
			// Pflaume
		} catch (ScriptException | FileNotFoundException e) {
			e.printStackTrace();
		}
	}

}

	

JavaScript array.js


var ArrayList = Java.type('java.util.ArrayList');
var list = new ArrayList();
list.add('Apfel');
list.add('Birne');
list.add('Pflaume');

for each (var element in list) {
	print(element); 
}
	

class NashornVererbung


package de.snowbits.js;

import java.io.FileNotFoundException;
import java.io.FileReader;

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

public class NashornVererbung  implements Runnable {
	
	public static void main(String[] args)  {
		try {
			ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
			engine.eval(new FileReader(NashornVererbung.class.getResource("vererbung.js").getPath()));

			// Obst
			// Apfel, Birne, Pflaume
		} catch (ScriptException | FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		System.out.println("Obst");		
	}

}
	

JavaScript vererbung.js


var Obst = Java.type('de.snowbits.js.NashornVererbung');
var Obstkorb = Java.extend(Obst);

var warenkorb = new Obstkorb() {
    run: function() {
        Java.super(warenkorb).run();
        print('Apfel, Birne, Pflaume');
    }
}

warenkorb.run();
	

Top


Example
Sitemap Kontakt Impressum