首页 > 解决方案 > 线程“主”java.lang.IllegalAccessError 中的异常:访问类失败

问题描述

所以我试图运行一个名为 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

标签: javaclassexceptionerror-handlingruntime-error

解决方案


Actual Issue

I got this exact same error* doing something very silly:

I tried to run the file as java {main-class}.java. That simple!

Instead, be sure to run it simply as java {main-class}.


*Specifically, the error format I had, like yours:

Exception in thread "main" java.lang.IllegalAccessError: failed to access class {pack.other-class} from class {pack.main-class} ({pack.other-class} is in unnamed module of loader 'app'; {pack.main-class} is in unnamed module of loader com.sun.tools.javac.launcher.Main$MemoryClassLoader @29f69090)

  at {pack.main-class}.{who-cares-where}
  at {pack.main-class}.{who-cares-why}
             <strong>. . .


Extra Advice

You can get a similarly annoying error on the same issue, namely inability to access packages in the same directory, if you only compile your {main-class}.

So instead of javac {directory}/{main-class}.java

Be sure to compile all of them at the same time, so there's no issue in cross-referencing:
  <code>javac {directory}/*.java


OP Specific

I downloaded from your Google Drive folder, to make sure you were also getting this error due to nothing more than a silly command-line snafu. However, I got a completely unrelated error, so I imagine you've since changed the files. However, I hope this at least serves someone else, if not you!


推荐阅读