[英]Getting Error Exception in thread “main” java.lang.IllegalAccessError:
[英]Exception in thread "main" java.lang.IllegalAccessError: failed to access class
所以我试图运行一个名为 CountdownTree.java 的特定文件,它继承了 package comp2402a4 中的一堆其他文件的函数。
这些都是我的导师提供的开始文件,我应该添加到这些文件中,运行这些文件不应该有任何错误。 我使用'javac comp2402a4/CountdownTree.java'编译它,它编译得很好,没有问题。 但是当我尝试使用'java comp2402a4/CountdownTree.java'运行它时,我得到了错误:
Exception in thread "main" java.lang.IllegalAccessError: failed to access class
comp2402a4.DefaultComparator from class comp2402a4.CountdownTree (comp2402a4.DefaultComparator is in
unnamed module of loader 'app'; comp2402a4.CountdownTree is in unnamed module of loader
com.sun.tools.javac.launcher.Main$MemoryClassLoader @21507a04)
at comp2402a4.CountdownTree.<init>(CountdownTree.java:26)
at comp2402a4.CountdownTree.main(CountdownTree.java:53)
我完全不知道是什么原因造成的,我真的很沮丧,因为我需要运行这个文件,这样我才能开始我的项目。 我尝试谷歌搜索,但无法弄清楚出了什么问题。 对于可能出现的问题,我非常感谢任何帮助。
CountdownTree.java:
package comp2402a4;
import java.util.Random;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* An unfinished implementation of an Countdown tree (for exercises)
* @author morin
*
* @param <T>
*/
public class CountdownTree<T> extends
BinarySearchTree<CountdownTree.Node<T>, T> implements SSet<T> {
// countdown delay factor
double d;
public static class Node<T> extends BSTNode<Node<T>,T> {
int timer; // the height of the node
}
public CountdownTree(double d) {
this.d = d;
sampleNode = new Node<T>();
c = new DefaultComparator<T>();
}
public boolean add(T x) {
Node<T> u = new Node<T>();
u.timer = (int)Math.ceil(d);
u.x = x;
if (super.add(u)) {
// add some code here
return true;
}
return false;
}
public void splice(Node<T> u) {
Node<T> w = u.parent;
super.splice(u);
// add some code here (we just removed u from the tree)
}
protected void explode(Node<T> u) {
// Write this code to explode u
// Make sure to update u.parent and/or r (the tree root) as appropriate
}
// Here is some test code you can use
public static void main(String[] args) {
Testum.sortedSetSanityTests(new SortedSSet<Integer>(new CountdownTree<Integer>(1)), 1000);
Testum.sortedSetSanityTests(new SortedSSet<Integer>(new CountdownTree<Integer>(2.5)), 1000);
Testum.sortedSetSanityTests(new SortedSSet<Integer>(new CountdownTree<Integer>(0.5)), 1000);
java.util.List<SortedSet<Integer>> ell = new java.util.ArrayList<SortedSet<Integer>>();
ell.add(new java.util.TreeSet<Integer>());
ell.add(new SortedSSet<Integer>(new CountdownTree<Integer>(1)));
ell.add(new SortedSSet<Integer>(new CountdownTree<Integer>(2.5)));
ell.add(new SortedSSet<Integer>(new CountdownTree<Integer>(0.5)));
Testum.sortedSetSpeedTests(ell, 1000000);
}
}
这是一个包含 package 中所有文件的文件夹,如果您想尝试运行它:
https://drive.google.com/drive/folders/1Cu0qNud7-1ACqLvyLahKiVVk0aHcLMEr?usp=sharing
我得到了这个完全相同的错误*做一些非常愚蠢的事情:
我尝试将文件作为java {main-class}.java
。 就这么简单!
相反,请务必将其简单地作为java {main-class}
运行。
*具体来说,我的错误格式,就像你的:
线程“主”java.lang.IllegalAccessError 中的异常:无法从 class
{pack.main-class}
类} 访问 class{pack.other-class}
{pack.other-class}
{pack.main-class}
在加载器 com.sun.tools.javac.launcher.Main$MemoryClassLoader @29f69090 的未命名模块中)在
{pack.main-class}
。{who-cares-where}
在{pack.main-class}
。{who-cares-why}
. . .
如果您只编译{main-class}
,您可能会在同一问题上遇到类似的烦人错误,即无法访问同一目录中的包。
所以代替javac {directory}/{main-class}.java
一定要同时编译所有的,这样交叉引用就没有问题了:
javac {directory}/*.java
我从您的 Google Drive 文件夹中下载,以确保您也收到此错误,这只不过是一个愚蠢的命令行问题。 但是,我得到了一个完全不相关的错误,所以我想你已经更改了文件。 但是,我希望这至少对其他人有用,如果不是你的话!
我只是在一个类似的问题上苦苦挣扎,我注意到无法访问的 class 缺少其public
修饰符(并且位于不同的包中)。 在我的例子中,类的编译方式有点不标准,所以这个问题出现在运行时而不是编译时(通常会被标记)。
在您的情况下, DefaultComparator
class 也缺少public
修饰符,但我不明白这有什么关系,因为这两个类都在同一个 package 中。 但是,正如您的错误消息所示,这两个类是由不同的 class 加载器加载的,对于这样一个简单的示例来说,这有点令人惊讶,所以我建议调查一下为什么会这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.