开发者

access a java class from within groovy

开发者 https://www.devze.com 2023-04-08 04:46 出处:网络
I have a simple java class: package test; class Hello { public static void main(String[] args) { System.out.println(\"Hi\");

I have a simple java class:

package test;
class Hello {
  public static void main(String[] args) {
    System.out.println("Hi");
  }
}

on which I do a

javac Hello.java

Problem: Now I would like to access this class from a groovy script (access.groovy) ...

import test.*
Hello.main(null)

but

groovy -cp . access.groovy

will result in a MissingPropertyException . What开发者_如何学编程 am I doing wrong?


Your class Hello needs to be declared as public to be accessible from other packages. As a dynamic language, Groovy can't identify such errors and ends up looking for a variable named Hello.

It's generally a bad idea to use wildcard imports; in this case, using import test.Hello; would have given you a better error message.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号