First, just to clear this up, I did not post this is the wrong place. I know the difference between "Java" and "Javascript"

I'm working on a project where I want to allow a simple, no intense programming required, way for users to create custom functionality. This is a simplified view of my program, but works for this discussion: My program works by running self contained chunks of Java (not javascript), but I want users to be able to define their own chunks of code to run in Javascript.

My user base tends to be techy, but they are not programmers. A full blown programming language would be more than they want to deal with. Javascript is much easier to use and get started with. You'll notice in my title I'm fine with any scripting language at all. The thing is, I didn't want to just pull some random language out of no where that I made up and force users to learn it. I want something more standard, that they might already know. Is there a way or a library I could use to define a customized javascript-esq interpreter in Java (I would need to replace the DOM with objects in my program). Or is there some standard Design Pattern to use? Performance is not a big deal, which is why I'm sticking with Java and not writing it in C.

Alternatively, is there a simple scripting standard that I can use. As in, some other standard scripting language or format that I didn't just pull out of thin air.

Recommended Answers

All 3 Replies

Recent Java versions have direct support for embedded scripting languages, including sharing data etc. Here's a tiny eg I had lying about.

// evaluate arbitrary jscript boolean expression...
ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript");
try {
   String condition = "a==b"; // expression to evaluate
   js.put("a", 2); // value for a
   js.put("b", 3); // value for b
   Object result = js.eval("var a,b;" + condition + ";");
   System.out.println(result);
} catch (ScriptException ex) {
   ex.printStackTrace();
}

http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/

Absolutely brilliant.

Here's some additional information I found from the page you linked to:

1) Along with integers, one can reference objects and call their functions from Javascript.

2) This works vice-versa as well, so Java can call functions and methods of JS objects.

3) Tighter integreation is avalable: JS can implement Java interfaces in order to define Java classes within JS.

That's right. (The little eg was just to give you an instant flavopur of it.) For your requirement it just coudn't be any better. Christmas amnd your birthday rolled into one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.