0

I have 4 files, Complex.java, Polynomial.java, Newton.java, and NewtonFractal.java whereby in each class we have:

  1. Complex sets up a data type Complex() which can be seen as a point in R^2 with a real and imaginary part.

  2. Polynomial sets up a polynomial of complex entries that is initialised by an array of complex entries.

  3. Newton which performs the Newton-Raphson algorithm that I have finished and I'm testing in main.

For some reason when I write:

Polynomial p = new Polynomial(coeff);

where coeff is of type Complex[]. I get the error

Exception in thread "main" java.lang.IllegalAccessError: failed to access class Polynomial from class Newton (Polynomial is in unnamed module of loader 'app'; Newton is in unnamed module of loader com.sun.tools.javac.launcher.Main$MemoryClassLoader @473b46c3) at Newton.main(Newton.java:169)

What's weird is I get no error for

Complex[] coeff = new Complex[] {new Complex(-1.0, 0.0), new Complex(), new Complex(), (1.0, 0.0)};

so the Complex class is being accessed fine just not the Polynomial class. I've deleted all previous class files, replaced them, and the locations of each loaders are:

  1. com.sun.tools.javac.launcher.Main$MemoryClassLoader@58c1c010 for Newton.java
  2. com.sun.tools.javac.launcher.Main$MemoryClassLoader@b62fe6d for Polynomial.java
  3. com.sun.tools.javac.launcher.Main$MemoryClassLoader@15eb5ee5 for Complex.java

which I got from running System.out.println("Insert Class Name".class.getClassLoader()); in main and compiling for each file. I could not run System.out.println(Polynomial.class.getClassLoader()); from the Newton or Complex file, but I could run System.out.println(Complex.class.getClassLoader()); in Newton giving: jdk.internal.loader.ClassLoaders$AppClassLoader@3d4eac69 for Newton.java. But I could not run the same code in the Complex file with Newton as the class. Also I can run getClassLoader() for Complex.java from Polynomial.java and I get jdk.internal.loader.ClassLoaders$AppClassLoader@3d4eac69. But I can't do the same for getClassLoader() for Polynomial.java from Complex.java giving:

Exception in thread "main" java.lang.IllegalAccessError: failed to access class Newton from class Polynomial (Newton is in unnamed module of loader 'app'; Polynomial is in unnamed module of loader com.sun.tools.javac.launcher.Main$MemoryClassLoader @b62fe6d) at Polynomial.main(Polynomial.java:135)

I'm new so I'm not sure what information is most useful to share so I tried to give everything but if anything else is needed don't hesitate to ask.

  • 1
    The presence of `MemoryClassLoader` indicates you're trying to [launch a single source file](https://openjdk.java.net/jeps/330) directly (e.g. `java Complex.java`). Yet you mention you have more than one source file. You need to compile all the source files then, assuming "Complex" is your main class, use `java your.package.Complex` (notice no ".java" extension). If that doesn't help, could you please provide a [mre] demonstrating the problem? – Slaw Mar 12 '20 at 17:44
  • Related; [java.lang.IllegalAccessError](https://stackoverflow.com/questions/7076414/java-lang-illegalaccesserror-tried-to-access-method) – Rans Mar 12 '20 at 17:47
  • @Slaw What does your.package refer to? For me all 3 files sit in Documents/University/MA117/Project2 so is it that? – John Miller Mar 12 '20 at 17:52
  • 1
    It's part the fully qualified name of the class. If your `Complex.java` source file has a `package com.example;` declaration at the top of the file then you'd use `java com.example.Complex`. If you have no package declaration then your source files are in the so-called default/unnamed package and you would simply use `java Complex`. As an aside, typically source files would be in directory structures mirroring the package structure, so the `com.example.Complex` class would be in the `/com/example/Complex.java` file. – Slaw Mar 12 '20 at 17:56
  • 1
    Perhaps, just declaring the classes `public` would help... – Holger Mar 20 '20 at 11:50

0 Answers0