package javascript;

 

import javax.script.ScriptEngineFactory;

import javax.script.ScriptEngineManager;

 

public class JavaScript {

 

    public static void main(String[] args) {

        ScriptEngineManager m = new ScriptEngineManager();

        for (ScriptEngineFactory factory : m.getEngineFactories()) {

            System.out.println(factory.getEngineName());

            System.out.println(factory.getLanguageName());

            System.out.println("--------------------");

        }

    }

   

}  

 

 

 

package javascript;

 

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.script.ScriptEngine;

import javax.script.ScriptEngineManager;

import javax.script.ScriptException;

 

public class JavaScript {

 

    public static void main(String[] args) {

        try {

            ScriptEngineManager m = new ScriptEngineManager();

            ScriptEngine e = m.getEngineByName("nashorn");

            e.eval("print('Hello World!')");

        } catch (ScriptException ex) {

            Logger.getLogger(JavaScript.class.getName()).log(Level.SEVERE, null, ex);

        }

    }

   

}

 

 

 

 

package javascript;

 

import javax.script.Invocable;

import javax.script.ScriptEngine;

import javax.script.ScriptEngineManager;

 

public class JavaScript2 {

 

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

    ScriptEngineManager e = new ScriptEngineManager();

    ScriptEngine nashorn = e.getEngineByName("nashorn");

    nashorn.eval("function sum(a, b) { return a+b; }");

    Invocable invocable = (Invocable) nashorn;

    Integer result = (Integer)invocable.invokeFunction("sum", 20, 23);

    System.out.println(result);

  }

}

 

 

 

package javascript;

 

import javax.script.*;

 

public class JSInterfaces {

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

        ScriptEngineManager e = new ScriptEngineManager();

    ScriptEngine nashorn = e.getEngineByName("nashorn");

        String script = "function run() {print('This is running in a thread!'); }";

        nashorn.eval(script);

        Invocable invocable = (Invocable) nashorn;

        Runnable runnable = invocable.getInterface(Runnable.class);

 

        Thread thread = new Thread(runnable);

        thread.start();

    }

}

 

 

function sum(a, b) {

    return a + b;

}

 

function multiply(a, b) {

    return a * b;

}

 

function max(a, b) {

    return Math.max(a, b);

}

 

print(sum(100, 15));

 

 

package javascript;

 

import java.util.Arrays;

import java.util.List;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.script.Invocable;

import javax.script.ScriptEngine;

import javax.script.ScriptEngineManager;

 

public class JavaScript {

 

    public static void main(String[] args) {

        try {

            ScriptEngineManager m = new ScriptEngineManager();

            ScriptEngine e = m.getEngineByName("nashorn");

            e.eval(new java.io.FileReader("src/javascript/helpers.js"));

            Invocable invocable = (Invocable) e;

            Integer result = (Integer)invocable.invokeFunction("sum", 20, 30);

            System.out.println(result);

        } catch (Exception ex) {

            Logger.getLogger(JavaScript.class.getName()).log(Level.SEVERE, null, ex);

        }

    }

   

}

 

 

function useJava() {

    var list = new java.util.ArrayList();

    list.add(8);

    list.add(20);

    list.add(88);

    list.add(21);

    return list.stream().mapToInt(function(i) {return i*i}).sum();

}

 

 

[1,2,3,4,5,6,7,8].filter(function(i) {return i%2==1})

 

 

list = new java.util.ArrayList();

list.add(1);

list.add(2);

list.add(3);

result = list.stream().mapToInt(function(i) {return i*i}).sum()

print(result)