0

I have a math class in a package mypackage. Now I want to import this class in MVEL and assign values to its property and access its method. I have written the following code but its giving error as

Exception in thread "main" [Error: unknown class or illegal statement: 
                      ^

Code is

ParserContext context = new ParserContext();
context.addImport("math",mypackage.MyMaths.class);//MyMaths.class is public
context.addInput("obj", mypackage.MyMaths.class);

String expression1 = "obj.a == 20";//a is public property

Serializable compiled1 = MVEL.compileExpression(expression1,context);

MVEL.executeExpression(compiled1);
cfi
  • 10,915
  • 8
  • 57
  • 103
user2764578
  • 61
  • 3
  • 10

1 Answers1

0

Please try with the below approach.

 public static void main(String[] args) {

        Map map = new HashMap();
        MyMaths mayMaths = new MyMaths();
        map.put("obj", mayMaths);

        String expression1 = "obj.a = 20";

        Serializable compiled1 = MVEL.compileExpression(expression1);

        MVEL.executeExpression(compiled1, map);

        System.out.println(mayMaths.getA());
    }

Output - 20

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116