【发布时间】:2020-07-14 22:26:26
【问题描述】:
所以我正在尝试运行一个名为 CountdownTree.java 的特定文件,该文件继承了 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);
}
}
如果你想尝试运行它,这里是一个包含包中所有文件的文件夹:
https://drive.google.com/drive/folders/1Cu0qNud7-1ACqLvyLahKiVVk0aHcLMEr?usp=sharing
【问题讨论】:
-
很难得到 IllegalAccessException。唯一可能的方法是,如果您在运行时的类从您的源 .java 文件更改。我的猜测是您可能包含了您的讲师版本的 DefaultComparator,并且在您的版本中,该类中的方法之一具有更广泛的访问规范(即公共而不是私有)
-
我不知道关于 SO 上近乎重复的正确协议,没有建议关闭它们,我没有。但是,我想指出有一个问题与here 完全相同的错误。出于这个原因,我粘贴了相同的响应,只是在底部添加了不同的“OP 特定”注释。仅适用于遇到相同错误的其他人!
标签: java class exception error-handling runtime-error