Tuesday, September 23, 2014

eval() in JAVA

Eienstien and Netwon beside me were worried about a requirement for a quick fix which otherwise required fair amount of time to do something silly. When they approached me I felt like John Dalton to tell them that it was very simple and laughed at their face, only to realize and feel humiliated. They wanted to create a dynamic condition, I can other wise tell that they wanted to execute an if statement whose condition is generated at the run time. I knew that their was a command called as “Eval” in javascript for ages to do it. Unfortunately these guys were not dumb and they wanted to find something in java. Pizz kirck bam bam – that’s how they knocked my head from all directions.


So here with a bumpy head, We found a way to do that in java which is as follows.( I definitely know that I got the information from another website. So don’t think about teasing me :O)


public class test {


                public static void main(String args[])        {


                    try {


                                String x="asd";

                                String y="bsd";


                                javax.script.ScriptEngineManager manager = new javax.script.ScriptEngineManager();

                                                javax.script.ScriptEngine engine = manager.getEngineByName("js");

                                                Object result = engine.eval("\""+x+"\".equalsIgnoreCase(\""+y+"\")");



                                } catch (javax.script.ScriptException e1) {

                                                // TODO Auto-generated catch block







Hope some one else finds this post after getting a bump on their head. :D