0

So I'm trying to run a particular file called CountdownTree.java that inherits functions from a bunch of other files in the package comp2402a4.

These were all starting files given by my instructor that I'm supposed to add to, and there shouldn't be any errors running these files. I compiled it using 'javac comp2402a4/CountdownTree.java' and it compiled fine with no problems. But when I try to run it using 'java comp2402a4/CountdownTree.java', I get the error:

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)

I have absolutely no idea what's causing this and I'm really frusterated because I need this file to run so I can start my project. I tried googling but couldn't figure out what's wrong. I would really appreciate any help as to what might be the problem.

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);
    }
}

Here's a folder with all the files in the package if you'd like to try running it:

https://drive.google.com/drive/folders/1Cu0qNud7-1ACqLvyLahKiVVk0aHcLMEr?usp=sharing

GilmoreGirling
  • 151
  • 1
  • 2
  • 7
  • It is hard to get the IllegalAccessException. The only way it is possible is if your classes at runtime are changed from your source .java files. My guess is that you are probably including your instructor's version of DefaultComparator, and that in your version one of the methods in that class has a more broad access specification (ie public rather than private) – ControlAltDel Apr 02 '20 at 20:20
  • I don't know the proper protocol on near-duplicates on SO, short of proposing to close them, which I haven't. However, I'd like to point out that there's a question with the same exact error [here](https://stackoverflow.com/questions/55795983/two-java-files-getting-illegalaccesserror-when-running-class-with-main-method-t/61527999#61527999). For that reason, I've pasted the same response, just with a different "OP Specific" note at the bottom. Just for the others who come across this page with the same error! – AviFS Apr 30 '20 at 16:19

2 Answers2

7

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}
             . . .


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:
  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!

AviFS
  • 336
  • 2
  • 12
  • This is a few years after your answer, but I am currently using Java for the first time and had this big assignment due and it wasn't working. You absolutely saved my life here! – Hercislife Feb 01 '23 at 23:33
1

I just struggled with a similar issue and I noticed that the class that couldn't be accessed was missing its public modifier (and was located in a different package). In my case, the way the classes were compiled was somewhat nonstandard, so this issue surfaced at runtime instead of at compile time (where this would normally be flagged).

In your case, the DefaultComparator class is also missing a public modifier, but I don't see how that would matter, since both classes are in the same package. However, as your error message indicates, the two classes are loaded by different class loaders, which is somewhat surprising for such a simple example, so I would suggest to investigate why that is.

raner
  • 1,175
  • 1
  • 11
  • 21