Boolean Expression Evaluation in Java

TJ- picture TJ- · Nov 15, 2009 · Viewed 16.6k times · Source

I'm looking for a relatively simpler (when compared with writing a parser) way to evaluate boolean expressions in Java, and I do not want to use the JEP library.

I have a String expression like: (x > 4 || x < 8 && p > 6) and my aim is to replace the variables with values.

Is there a way by which I can evaluate this expression?

Bear in mind that this can be any level deep so writing a parser would be very complex.

Answer

Venkat Sadasivam picture Venkat Sadasivam · Nov 15, 2009

Use Apache Commons Jexl; which is exactly designed for such requirement.

http://commons.apache.org/jexl/