[英]access a java class from within groovy
I have a simple java class: 我有一个简单的java类:
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) ... 问题:现在我想从一个groovy脚本访问这个类(access.groovy)...
import test.*
Hello.main(null)
but 但
groovy -cp . access.groovy
will result in a MissingPropertyException
. 将导致
MissingPropertyException
。 What am I doing wrong? 我究竟做错了什么?
Your class Hello
needs to be declared as public to be accessible from other packages. 您的类
Hello
需要声明为public才能从其他包中访问。 As a dynamic language, Groovy can't identify such errors and ends up looking for a variable named Hello
. 作为一种动态语言,Groovy无法识别此类错误,最终会查找名为
Hello
的变量。
It's generally a bad idea to use wildcard imports; 使用通配符导入通常是个坏主意; in this case, using
import test.Hello;
在这种情况下,使用
import test.Hello;
would have given you a better error message. 会给你一个更好的错误信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.