How to execute code contained in a variable String in groovy?

There is a variable and code in it. (Code the user enters in the textArea) How to run this code in groovy. In this code, you can call the methods of classes in java
July 8th 19 at 16:06
1 answer
July 8th 19 at 16:08
Solution
Is it possible in this code to access the methods of classes written in java? - selmer.Johns89 commented on July 8th 19 at 16:11
: well, there is a constructor to which you pass classloader, I think, no problems.. I do not write on Groovy - I asked Google for five seconds.

in General, you nabizhali with questions. no specifics. in the previous advised the first decision you have taken.
For Java, for example, BeanShell is. There's too much that can. You have a textarea in which language the code at all? - Montana_Abshire commented on July 8th 19 at 16:14
Java. I am just a beginner and not familiar with many things of which I spoke. But the application I would like to write, so looking for a solution to this problem. I think experienced programmers not bad advice) - selmer.Johns89 commented on July 8th 19 at 16:17
: What would you recommend for this task? - selmer.Johns89 commented on July 8th 19 at 16:20
What have tried yourself www.beanshell.org/examples.html - Montana_Abshire commented on July 8th 19 at 16:23
Thank you for spent you time on me. Helped) - selmer.Johns89 commented on July 8th 19 at 16:26
: Wellcome - Montana_Abshire commented on July 8th 19 at 16:29
: I think I'm an idiot(... I can't find on this site example how to run the code stored in the string - selmer.Johns89 commented on July 8th 19 at 16:32
: well, EMA.. a little Google-fu do not hurt, young Padawan :) www.beanshell.org/javadoc/bsh/Interpreter.html - Montana_Abshire commented on July 8th 19 at 16:35
: Thank you so much for tolerance. Sorry for the stupidity, I am 14 years old and unfortunately the experience of finding information on the Internet yet) - selmer.Johns89 commented on July 8th 19 at 16:38
: votated! 14! respect and uvazhuha! in General, the link that I gave, there is the eval method is. it is possible to transfer bensel the script as a string. Go for it! - Montana_Abshire commented on July 8th 19 at 16:41
: How to execute code which are called by my methods? - selmer.Johns89 commented on July 8th 19 at 16:44
: your methods is an instance of your object you already created, probably, right?
MyObject mObj = new MyObject(); // this is already somewhere there.
now you have an instance of the interpreter bensel Interpreter i = new Interpreter();
and you can do i.set("scriptObj", mObj); read more here www.beanshell.org/javadoc/bsh/Interpreter.html#set...,%20java.lang.Object)

and then in the script bensel you will have the object scriptObj, with the methods of MyObject. and call them out. - Montana_Abshire commented on July 8th 19 at 16:47
: I have a task to run such code: mTrueButton.setEnabled(false); (Android) how to convey to him what this button directly in the application button changed this setting? - selmer.Johns89 commented on July 8th 19 at 16:50
well, you're fouled.. "this button" in Android using findViewById (Button myButton = (Button) findViewById(R. id.my_button);), then, as I suggested above, but there is a link broken toaster.. hope you'll figure out how to insert it to jump. - Montana_Abshire commented on July 8th 19 at 16:53
: I wrote the Interpreter bsh = new Interpreter();
try {
Button myButton = (Button) findViewById(R. id.true_button);
bsh.set("scriptObj", myButton);
bsh.eval("myButton.setEnabled(false);");
} catch(Exception e) {}
But gives an error - selmer.Johns89 commented on July 8th 19 at 16:56
Gives NoClassDefFoundError - selmer.Johns89 commented on July 8th 19 at 16:59
: first, the string to eval is already a script bihela, it's your myButton is called scriptObj (scriptObj.setEnabled(false)), and secondly, the logs necessary to fully see the entire stack trace. it is written in detail where exactly NoClassDefFoundError - Montana_Abshire commented on July 8th 19 at 17:02
It's all good! Thank you very much! - selmer.Johns89 commented on July 8th 19 at 17:05
: well done, congratulations! :) - Montana_Abshire commented on July 8th 19 at 17:08

Find more questions by tags Java