45

I wanted to build a small product in which I wanted to give a kind of feature in which user can write a script language kind of JavaScript.

And also from JavaScript able to build objects and calling methods on them.

Is there any framework for this?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Dungeon Hunter
  • 19,827
  • 13
  • 59
  • 82
  • @Felix: as cool as it is, Node.js has nothing to do with Java or web browsers, right? – maerics Sep 20 '11 at 15:47
  • @maerics Right, but it can provide a console in which to tinker. And it can run scripts. And the OP didn't mention anything about web browsers :) – Felix Sep 20 '11 at 15:51
  • 1
    @Felix Then the idea of a "JavaScript console that every browser has isn't relevant, and node.js isn't Java, so... doesn't seem very helpful either. – Dave Newton Sep 20 '11 at 16:33
  • 1
    Java 8 is providing a new JavaScript engine, Nashorn, to replace Rhino. See the Nashorn blog https://blogs.oracle.com/nashorn/ – Richard Chambers Mar 22 '14 at 16:22
  • @RichardChambers good to know thanks richard. Turn it as an answer so that it can be useful for the users – Dungeon Hunter Mar 22 '14 at 18:58

4 Answers4

38

Rhino is what you are looking for.

Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically embedded into Java applications to provide scripting to end users.

Update: Now Nashorn, which is more performant JavaScript Engine for Java, is available with jdk8.

Update 2022: Nashorn was deprecated in Java 11, then eventually removed in Java 15.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
kdabir
  • 9,623
  • 3
  • 43
  • 45
16

Java includes a scripting language extension package starting with version 6.

See the Rhino project documentation for embedding a JavaScript interpreter in Java.

[Edit]

Here is a small example of how you can expose Java objects to your interpreted script:

public class JS {
  public static void main(String args[]) throws Exception {
    ScriptEngine js = new ScriptEngineManager().getEngineByName("javascript");
    Bindings bindings = js.getBindings(ScriptContext.ENGINE_SCOPE);
    bindings.put("stdout", System.out);
    js.eval("stdout.println(Math.cos(Math.PI));");
    // Prints "-1.0" to the standard output stream.
  }
}
maerics
  • 151,642
  • 46
  • 269
  • 291
12

You can use ScriptEngine, example:


public class Main {
    public static void main(String[] args) {
        StringBuffer javascript = null;
        ScriptEngine runtime = null;

        try {
            runtime = new ScriptEngineManager().getEngineByName("javascript");
            javascript = new StringBuffer();

            javascript.append("1 + 1");

            double result = (Double) runtime.eval(javascript.toString());

            System.out.println("Result: " + result);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

Eder
  • 1,874
  • 17
  • 34
0

I just wanted to answer something new for this question - J2V8.

Author Ian Bull says "Rhino and Nashorn are two common JavaScript runtimes, but these did not meet our requirements in a number of areas:

Neither support ‘Primitives‘. All interactions with these platforms require wrapper classes such as Integer, Double or Boolean. Nashorn is not supported on Android. Rhino compiler optimizations are not supported on Android. Neither engines support remote debugging on Android.""

Highly Efficient Java & JavaScript Integration

Github link

Ashok M A
  • 478
  • 6
  • 14