Java Scripting Engine importing my classes does not work

Posted by Ayman on Stack Overflow See other posts from Stack Overflow or by Ayman
Published on 2010-04-25T09:10:05Z Indexed on 2010/04/25 9:13 UTC
Read the original article Hit count: 389

Filed under:
|

A code is worth 1000 words of explaining it :-)

package jasim;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class JSTest {

    public static void main(String[] args) throws ScriptException {
        ScriptEngine jse = new ScriptEngineManager().getEngineByExtension("js");

        jse.eval("println(new jasim.JSTest().toString)");

    }

    @Override
    public String toString() {
        return "JSTest Object";
    }
}

This code will fail with the below exception:

Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "jasim" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1

How do I import my own classes into the ScriptEngine?

© Stack Overflow or respective owner

Related posts about java

Related posts about scriptengine