【问题标题】:Exception in thread "main" java.lang.IllegalAccessError: failed to access class线程“主”java.lang.IllegalAccessError 中的异常:访问类失败
【发布时间】: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


【解决方案1】:

实际问题

我在做一些非常愚蠢的事情时遇到了同样的错误:

我试图以java {main-class}.java 运行该文件。就这么简单!

请务必以java {main-class} 的形式运行它。


*具体来说,我遇到的错误格式,和你的一样:

线程“main”java.lang.IllegalAccessError 中的异常:无法从类{pack.main-class} 访问类{pack.other-class}{pack.other-class} 在加载程序'app' 的未命名模块中;{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


OP 特定

我从您的 Google Drive 文件夹下载,以确保您也收到此错误,这仅仅是因为一个愚蠢的命令行问题。但是,我得到了一个完全不相关的错误,所以我想你已经更改了文件。但是,我希望这至少可以服务于其他人,如果不是你的话!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 2013-03-30
    • 1970-01-01
    • 2017-10-30
    • 2013-11-02
    • 2016-04-04
    相关资源
    最近更新 更多