调用Java方法从Groovy

huangapple go评论118阅读模式
英文:

Invoke java method from Groovy

问题

我正在研究Groovy,并且尝试在Groovy中绑定Java对象并从脚本中运行方法。

示例:

  1. public class Toaster {
  2. private int toasted = 0;
  3. public Toaster() {
  4. super();
  5. }
  6. public void toast() {
  7. System.out.println(++toasted);
  8. }
  9. }

在Java的主程序中:

  1. Toaster toaster = new Toaster();
  2. Binding binding = new Binding();
  3. binding.setVariable("toaster", toaster);
  4. GroovyShell shell = new GroovyShell(binding);
  5. shell.evaluate("toaster.toast();");

这显然无法正常工作,那么我需要在这里做些什么才能够在Groovy中使用Java的烤面包机对象呢?

英文:

I'm looking into Groovy, and I'm trying to bind java objects in groovy and run methods from the script.

Example:

  1. public class Toaster{
  2. private int toasted=0;
  3. public Toaster() {
  4. super();
  5. }
  6. public void toast(){
  7. System.out.println(++toasted);
  8. }
  9. }

And in the java main I have:

  1. Toaster toaster = new Toaster();
  2. Binding binding = new Binding();
  3. binding.setVariable("toaster", toaster);
  4. GroovyShell shell = new GroovyShell(binding);
  5. shell.evaluate("toaster.toast();");

This obviously doesn't work, so what do I need to do here to be able to use the java toaster object from groovy?

答案1

得分: 2

以下是你提供的代码的翻译部分:

A.java:

  1. import groovy.lang.GroovyShell;
  2. import groovy.lang.Binding;
  3. public class A{
  4. public static class Toaster{
  5. private int toasted=0;
  6. public void toast(){
  7. System.out.println(++toasted);
  8. }
  9. }
  10. public static void main(String[] arg){
  11. Toaster toaster = new Toaster();
  12. Binding binding = new Binding();
  13. binding.setVariable("toaster", toaster);
  14. GroovyShell shell = new GroovyShell(binding);
  15. shell.evaluate("toaster.toast();"); // < prints 1
  16. shell.evaluate("toaster.toast();"); // < prints 2
  17. shell.evaluate("toaster.toast();"); // < prints 3
  18. shell.evaluate("toaster.toast();"); // < prints 4
  19. }
  20. }

编译:

  1. javac -cp %GROOVY_HOME%/lib/* A.java

运行:

  1. java -cp %GROOVY_HOME%/lib/*;. A
英文:

your code works fine if you remove all typos...

A.java:

  1. import groovy.lang.GroovyShell;
  2. import groovy.lang.Binding;
  3. public class A{
  4. public static class Toaster{
  5. private int toasted=0;
  6. public void toast(){
  7. System.out.println(++toasted);
  8. }
  9. }
  10. public static void main(String[] arg){
  11. Toaster toaster = new Toaster();
  12. Binding binding = new Binding();
  13. binding.setVariable(&quot;toaster&quot;, toaster);
  14. GroovyShell shell = new GroovyShell(binding);
  15. shell.evaluate(&quot;toaster.toast();&quot;); // &lt; prints 1
  16. shell.evaluate(&quot;toaster.toast();&quot;); // &lt; prints 2
  17. shell.evaluate(&quot;toaster.toast();&quot;); // &lt; prints 3
  18. shell.evaluate(&quot;toaster.toast();&quot;); // &lt; prints 4
  19. }
  20. }

compile:

  1. javac -cp %GROOVY_HOME%/lib/* A.java

run:

  1. java -cp %GROOVY_HOME%/lib/*;. A

答案2

得分: 0

Groovy和Java将被编译成相同的字节码,保存为.class文件。因此,您不需要Groovy shell。我不确定您在这里的具体安排。但如果它们需要同时编译,我建议您使用Groovy的编译器groovyc进行编译,以便它可以识别Java代码的一侧。

英文:

Groovy and java will be compiled to the same bytecode as .class file. So, you don't need groovy shell. I am not sure what your arrangement here. But if they both need to be compiled at the same time, I suggest you use Groovy's compiler groovyc to compile, so it can recognize Java side of the code.

huangapple
  • 本文由 发表于 2020年9月18日 01:04:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63942931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定