Skip to content
Advertisement

Evaluate groovy expression in java class

I am looking to evaluate math expressions from String values using groovy in java code. I have created groovy script like below:

JavaScript

The class MyObject is defined as:

JavaScript

The code to evaluate the expression is:

JavaScript

I keep getting the below exception when I try to run the code:

JavaScript

Please suggest the correct approach to achieve this

Advertisement

Answer

i’m using groovy to implement main but it’s not far from java.


option 1 : use closures instead of functions

JavaScript

option 2: use script inheritance

JavaScript

option 3: groovy itself could be much shorter and probably you don’t need over-complicated solution

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement