2

Basically I want to be able to do be able to parse a JavaScript-like syntax. For example:

var results = system.function('example');  

if(results == "hello") {  
   console_print("ding dong.");  
}  

So basically you get it, results will be a "string" and system.function will call a Java method, and those results will go into the results string.

I want to be able to do basic math, and validation in this as well, but I want this done in Java. Any ideas?

1
  • This will sound silly, but I want to call a JavaScript file (file.js) and run functions from it. Commented Aug 27, 2011 at 11:49

2 Answers 2

1

If you're willing to use JavaScript (not just JavaScript-like), and you're using Java 1.6+, then you can use the Scripting API:

import javax.script.*;

public class Main {

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

    String source =
        "var system = new Array();                       \n" +
        "system['foo'] = function(s) { return 'hello'; } \n" +
        "                                                \n" +
        "var results = system.foo('example');            \n" +
        "                                                \n" +
        "if(results == \"hello\") {                      \n" +
        "  print(\"ding dong.\");                        \n" +
        "}                                               \n";

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
    engine.eval(source);
  }
}

which will print:

ding dong.

to the console.

And to invoke Java methods from the scripting language, you could give BeanShell a try:

package test;

import bsh.Interpreter;

public class Main {

  public static void foo(Object param) {
    System.out.println("From Java, param=" + param);
  }

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

    Interpreter i = new Interpreter();
    i.eval("a = 3 * 5; test.Main.foo(a);");
  }
}

which will print:

From Java, param=15

The core interpreter JAR of BeanShell is only 143 KB: more lightweight than that will be difficult, IMO.

Sign up to request clarification or add additional context in comments.

6 Comments

@Kiers: how did u format that string with \n, " and +? manually or any shortcut?
@Kowser, I created the JavaScript snippet using a plain text editor and ran it through a JavaScript interpreter. When it ran without any problem, I simply pasted the code inside the empty string literal: String source = ""; and IntelliJ made sure the quotes were properly escaped and added line-breaks for me.
This is not exactly what I wanted, I want to be able to route it to functions in Java.. or can you do that? If you can show a quick example that'd be fantastic.
@Mark, you said: "and system.function will call a function,". Perhaps you should edit you question and add this extra information that it's not just a function you want to call, but a Java method.
That's great, last question. Do you know a way to "Jail" as you could say to the test.Main.foo class, so it could not be re-written to access say test.security.Decrypt, or, test.security.Encrypt?
|
1

I think you can use Rhino for this. Rhino is an javascript engine that can be embedded in java.

2 Comments

Is there a more lightweight version of this?
@Mark, this is the same as my suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.