FAQ Database Discussion Community

How can I access a static-method only class via ScriptEngine?

I have a class Foo that has only one static method (of note), called bar(). I need to access bar() via a runtime script (in this case Javascript). final ScriptEngineManager factory; factory = new ScriptEngineManager(); factory.put("foo", new Foo()); The above works, but it seems a bit odd to me. Specifically...

ReferenceError in ScriptEngine eval

I'm using the JavaScript engine in JDK 7. Is there a way to evaluate an expression like a||b to false if a and b are undefined, instead of throwing the exception ReferenceError: "a" is not defined.?

Java Scripting API — Property from JavaScript object is always null

Below is the code I am running in my Java compiler: ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("JavaScript"); engine.put("person", "{name: 'Bob', favoriteColor: 'red'}"); System.out.println(engine.get("person.name")); I would expect this to evaluate to "Bob", but instead it gives me null. If I try printing just the user object, it properly...