静态导入仅来自类和接口。

时间:2022-09-25 12:07:01

My code compiles fine in Eclipse, but when I try to compile from the commandline (via our ruby-based buildr system), I get this error message:

我的代码在Eclipse中编译得很好,但是当我试图从命令行(通过基于ruby的buildr系统)进行编译时,我得到了以下错误消息:

static import only from classes and interfaces

Suggesting that static import of public static fields is not permitted. What should I look for to help diagnose this problem? How can I fix it?

建议不允许静态导入公共静态字段。我应该寻找什么来帮助诊断这个问题?我怎样才能修好它呢?

Update: per @Ted's request, the constant declaration in the referenced file:

更新:根据@Ted的请求,引用文件中的常量声明:

public static final String NULL = "<NULL>";

and the (bowdlerized) reference in the referring file:

和参考文件中的(bowdlerized)引用:

import static my.path.MyClass.NULL;

2 个解决方案

#1


9  

My guess is that Eclipse and buildr are using either different Java compiler versions or different compiler flags. There's a bug in the Java 7 compiler (bug ID: 715906) that generates this error when you statically import specific fields. The work-around is to use a wildcard static import. So instead of:

我的猜测是Eclipse和buildr使用了不同的Java编译器版本或不同的编译器标志。Java 7编译器中有一个bug (bug ID: 715906),当静态地导入特定字段时,它会生成这个错误。解决方法是使用通配符静态导入。而不是:

import static package.Class.staticField;

do this:

这样做:

import static package.Class.*;

#2


7  

Late answer but I just got a similar issue and figured it out. I'll post in case it helps anyone else who finds this page...

虽然回答晚了,但我发现了一个类似的问题。如果它能帮助其他找到这个页面的人,我将发布它。

I got a similar error when, after a big merge and refactor, I accidentally put a test class into src/main/java instead of src/test/java. Since the JUnit dependency was scope=tests, it didn't work in pure maven. Maybe you are having the same issue

在一次大的合并和重构之后,我不小心将一个测试类放入了src/main/java,而不是src/test/java。由于JUnit依赖项是scope=tests,所以它不能在纯maven中工作。也许你也有同样的问题

#1


9  

My guess is that Eclipse and buildr are using either different Java compiler versions or different compiler flags. There's a bug in the Java 7 compiler (bug ID: 715906) that generates this error when you statically import specific fields. The work-around is to use a wildcard static import. So instead of:

我的猜测是Eclipse和buildr使用了不同的Java编译器版本或不同的编译器标志。Java 7编译器中有一个bug (bug ID: 715906),当静态地导入特定字段时,它会生成这个错误。解决方法是使用通配符静态导入。而不是:

import static package.Class.staticField;

do this:

这样做:

import static package.Class.*;

#2


7  

Late answer but I just got a similar issue and figured it out. I'll post in case it helps anyone else who finds this page...

虽然回答晚了,但我发现了一个类似的问题。如果它能帮助其他找到这个页面的人,我将发布它。

I got a similar error when, after a big merge and refactor, I accidentally put a test class into src/main/java instead of src/test/java. Since the JUnit dependency was scope=tests, it didn't work in pure maven. Maybe you are having the same issue

在一次大的合并和重构之后,我不小心将一个测试类放入了src/main/java,而不是src/test/java。由于JUnit依赖项是scope=tests,所以它不能在纯maven中工作。也许你也有同样的问题