0

I want to be able to pass an object into ScriptEngine via put() and be able to access its properties and methods from within the scripting engine.

e.g

public class MyClass {
    String getName() { return "abc"; }
}

MyClass my = new MyClass();
engine.put("abc", my);

How can I do this?

ycomp
  • 8,316
  • 19
  • 57
  • 95
  • First of all I guess you forgot a `new` for `new MyClass()`? If you have that, you can easily access the object, the how depends on the engine you are using. I have an example in my question: http://stackoverflow.com/questions/42338239/access-variable-of-scriptcontext-using-nashorn-javascript-engine-java-8 - maybe that helps. I'm using a context though... – Philipp Feb 26 '17 at 07:01
  • @Philipp sorry, I keep switching between Java and Kotlin all day (Kotlin has no `new`) – ycomp Feb 26 '17 at 07:03
  • @Philipp accessing is not a problem, but I can't figure out how to cast. I'm using groovy and javascript. – ycomp Feb 26 '17 at 07:05
  • What exception do you get? You should be able to simple access the methods of `my` without any exceptions, if they are `public`. Could you post your script (in Groovy and JavaScript). – Philipp Feb 26 '17 at 07:07
  • @Philipp i'm not sure the syntax so I tried in javascript both (`print(my.getName())` which gives an exception about it not being a function and `print(my.name)` which prints `undefined`. In groovy it seemed to think it was a `String` and so the exception said that `String` doesn't have that property... my groovy syntax was `println "$my.name"` - however, I put the object as the actual object, I don't convert it to a String – ycomp Feb 26 '17 at 07:17

1 Answers1

1

Here is a complete working example with JavaScript. As I mentioned in the comment, you have to make sure that your methods are public.

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

public class JavaScriptEngineSample {

    public static class MyClass {
        private String name;

        public String getName() { return name; }
        public void setName(final String name) { this.name = name; }
    }

    public static void main(final String[] args) throws ScriptException {
        final MyClass my = new MyClass();
        my.setName("Input");

        final ScriptEngineManager factory = new ScriptEngineManager();

        // you could also use 'javascript' here, I'm using 'nashorn' to visualize that I'm using the new Java 8 Engine
        final ScriptEngine engine = factory.getEngineByName("nashorn");

        engine.put("my", my);

        String script = "java.lang.System.out.println(my.getName());";
        script += "my.setName('Output');";

        engine.eval(script);

        System.out.println(my.getName());
    }
}
Philipp
  • 1,289
  • 1
  • 16
  • 37
  • thanks, it works.. I then removed the static and moved MyClass to another package in the same project. I get a ClassNotFoundException... I was wondering what I need to do so that the ScriptEngine knows where to find the class? - I'm thinking this might be at the root of my problems in my real project, even if the exceptions or error messages are not 'ClassNotFoundException' there – ycomp Feb 26 '17 at 10:59
  • That's normally not a problem, as long as the class is public and available on your class-path. I just kept it in one file for simplicity. Can you post a not running example? – Philipp Feb 27 '17 at 01:24
  • here is a screenshot of the project.. ignore the other background tabs..https://ibb.co/fPaNbF – ycomp Feb 27 '17 at 09:21
  • There seems to be another problem. The error is not about the `MyClass` it is about the `JavaScriptEngineSample` which cannot be found. So your `main` method is never executed! – Philipp Feb 28 '17 at 07:26
  • ah ok, thanks Philipp - I restarted the IDE, didn't touch anything - pressed the run button and now it works fine.I'm still not sure what is going on in my actual project but at least now I know it is supposed to work, and I can investigate more. When I posted I wasn't sure if it was supposed to work, especially because one exception was telling me it was converting the put variable to a String (even though my code didn't do any such conversion) – ycomp Feb 28 '17 at 07:50