java 仅从类和接口静态导入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11782076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
static import only from classes and interfaces
提问by Carl Manaster
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 的构建器系统)进行编译时,我收到以下错误消息:
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;
回答by Ted Hopp
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 和构建器使用不同的 Java 编译器版本或不同的编译器标志。Java 7 编译器中存在一个错误(错误 ID:715906),当您静态导入特定字段时会生成此错误。解决方法是使用通配符静态导入。所以而不是:
import static pkg.Class.staticField;
do this:
做这个:
import static pkg.Class.*;
回答by Collin Peters
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 中不起作用。也许你有同样的问题
回答by M Watson
I also had this error and my issue turned out to be a wayward static import of a junit 4 package in my test source file.
我也有这个错误,我的问题原来是我的测试源文件中 junit 4 包的任性静态导入。
I had the following:
我有以下几点:
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;
I removed the import static org.junit.Assert.fail;
(no idea how I managed to get that in there in the first place) and all is now working.
我删除了import static org.junit.Assert.fail;
(不知道我是如何设法将它放在首位的),现在一切正常。
回答by VenomVendor
I accidentally set test
directory as source. And Test sources were considered as source files.
我不小心将test
目录设置为源。并且测试源被视为源文件。
sourceSets.main.java.srcDirs 'src'
| -- src
| -- main
| -- test
| -- 源文件
| -- 主要
| - 测试
Fix:
使固定:
sourceSets.main.java.srcDirs 'src/main'