Java 此错误的原因:由于所需库的限制,无法访问类型测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23753214/
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
Cause of this error: The type Test is not accessible due to restriction on required library?
提问by blazonix
**Description**
Access restriction: The type Test is not accessible due to restriction on required library
**Resource**
/Applications/Eclipse Java/plugins/org.junit_4.11.0.v201303080030/junit.jar
**Path**
AdditionTest2.java
**Location**
/Practice/src/testing line 5
**Type**
Java Problem
I've seen the answers to these other SO threads:
我已经看到了这些其他 SO 线程的答案:
Access restriction on class due to restriction on required library
Access restriction on class due to restriction on required library rt.jar?
and I've applied the fix - Build Path --> Remove System Library --> Add it back in, and it works. However, I still have two problems:
我已经应用了修复程序 - 构建路径 --> 删除系统库 --> 重新添加它,它可以工作。但是,我仍然有两个问题:
1.Why is the code still highlighted in red?
1.为什么代码还是红色高亮?
Btw, the code is as follows
Btw,代码如下
package testing;
import static org.junit.Assert.*;
import org.junit.Test;
public class AdditionTest2 {
// @Test is underlined red
@Test
public void testAddition() {
Addition add = new Addition();
int[] numbers = {1, 2};
// this line is underlined red
assertEquals("1 + 2 must be 3", 3, Addition.add(numbers));
}
}
2.What caused this error? Again, I've read the two threads, but I still don't understand it.
2.是什么导致了这个错误?再次,我已经阅读了两个线程,但我仍然不明白。
Thanks!
谢谢!
Baggio
巴乔
EDIT: Actually, I stand corrected - it DOESN'T work. I'm not too familiar with testing in eclipse, but under the JUnit tab, nothing happens.
编辑:实际上,我已经纠正了 - 它不起作用。我对 eclipse 中的测试不太熟悉,但是在 JUnit 选项卡下,什么也没有发生。
EDIT 2: Build Path image if that helps any
编辑 2:如果有帮助,请构建路径图像
EDIT 3: Build Path image for JUnit 3 and JUnit 4
编辑 3:为 JUnit 3 和 JUnit 4 构建路径图像
EDIT 4 + Solution: Actually, the testing code with JUnit 4 doesn't run at all - when I click Run --> Run As it didn't run - I tried to remove JUnit 3 from the Build Path, and it works now! Except now I have the other problem of not knowing why the test failed, but that's another problem.
编辑 4 + 解决方案:实际上,JUnit 4 的测试代码根本没有运行 - 当我单击运行 --> 运行时没有运行 - 我试图从构建路径中删除 JUnit 3,它现在可以工作!除了现在我还有另一个问题是不知道为什么测试失败,但这是另一个问题。
回答by Aaron Digulla
Eclipse is based on OSGi. OSGi has a notion of "yes, you can see this class but you better don't use it." It's an extension of private
classes.
Eclipse 基于 OSGi。OSGi 有一个概念:“是的,您可以看到这个类,但最好不要使用它。” 它是private
类的扩展。
The general idea is that code from someone contains public API and it contains classes which are public because the Java compiler requires it but they aren't part of the public API.
一般的想法是来自某人的代码包含公共 API,它包含公共类,因为 Java 编译器需要它,但它们不是公共 API 的一部分。
Examples are classes in the Java runtime which are in the com.sun
package. Since only Oracle's version of Java contains them, it would be bad for you to use them. That's why Eclipse developers added the check to their compiler.
示例是com.sun
包中的 Java 运行时中的类。由于只有 Oracle 的 Java 版本包含它们,因此使用它们对您不利。这就是 Eclipse 开发人员将检查添加到他们的编译器的原因。
In your case, the error message seems odd. junit.jar
doesn't contain private API. My guess is that you somehow pulled in the wrong JUnit JAR (ie. one which Eclipse thinks is private and only accessible for the IDE).
在您的情况下,错误消息似乎很奇怪。junit.jar
不包含私有 API。我的猜测是您以某种方式引入了错误的 JUnit JAR(即 Eclipse 认为是私有的并且只能由 IDE 访问的 JAR)。
Try to use Built Path--> Add Libraries
to add "JUnit" to your project instead of manually adding a JAR.
尝试使用Built Path--> Add Libraries
将“JUnit”添加到您的项目中,而不是手动添加 JAR。
[EDIT]If you use Built Path--> Add Libraries
to add the JUnit JAR, then this error shouldn't happen. In fact, it shouldn't be an error in the first place - Access restrictions are warnings.
[编辑]如果您使用Built Path--> Add Libraries
添加 JUnit JAR,则不应发生此错误。事实上,它首先不应该是错误 - 访问限制是警告。
Try to reset your compiler options to the default and try again.
尝试将编译器选项重置为默认值,然后重试。
回答by Stefan
I had the same issue and could resolve it by removing junit from the manifest.mf files (using the dependency tab) in all my interacting plugin projects and then adding junit with the alternative aproach
Built Path ... --> Add Library... --> JUnit
as already described in the previous post by Aaron.
我遇到了同样的问题,可以通过从我所有交互插件项目中的 manifest.mf 文件(使用依赖项选项卡)中删除 junit 来解决它,然后使用替代方法
构建路径添加 junit ... --> 添加库.. . --> JUnit 已经在 Aaron 的前一篇文章中描述过。
回答by jstricker
I ran into a very similar issue, and I was able to resolve it by updating an entry in my Eclipse project's classpath file that was pointing to an older version of JUnit.
我遇到了一个非常相似的问题,我能够通过更新我的 Eclipse 项目的类路径文件中的一个条目来解决它,该条目指向旧版本的 JUnit。
I changed
我变了
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/>
to
到
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
回答by Astro
This is probably an obvious but if you're using spring framework make sure you add the spring library to the build path as well.
这可能是显而易见的,但如果您使用的是 spring 框架,请确保将 spring 库也添加到构建路径中。
Download spring library such as spring-framework-4.1.6.RELEASE-dist.zip - unzip to a folder on your laptop - C:\springLib\
下载 spring-framework-4.1.6.RELEASE-dist.zip 等 spring 库 - 解压到笔记本电脑上的文件夹 - C:\springLib\
Then add a User Library to the build path and point to this lib
然后在构建路径中添加一个用户库并指向这个库