Search code examples
javaprocessing

Using processing java , java.lang.IllegalAccessException


When running the processing code below:

package model;

import processing.core.*;

public class UsingProcessing extends PApplet {

    // The argument passed to main must match the class name
    
    // method for setting the size of the window
    public void settings(){
        size(500, 500);
    }

    // identical use to setup in Processing IDE except for size()
    public void setup(){
        background(0);
        stroke(255);
        strokeWeight(10);
    }

    // identical use to draw in Processing IDE
    public void draw(){
        line(0, 0, 500, 500);
    }
    public static void main(String[] args) {
     PApplet.main("model.UsingProcessing");
   }

}

I get an java runtime error.

The error is below. Specifically it states "class processing.core.PApplet (in module core) cannot access class model.UsingProcessing (in module Test) because module Test does not export model to module core"

java.lang.RuntimeException: java.lang.IllegalAccessException: class processing.core.PApplet (in module core) cannot access class model.UsingProcessing (in module CovidSchoolSimulation) because module CovidSchoolSimulation does not export model to module core
    at core/processing.core.PApplet.runSketch(PApplet.java:10227)
    at core/processing.core.PApplet.main(PApplet.java:10072)
    at core/processing.core.PApplet.main(PApplet.java:10054)
    at CovidSchoolSimulation/model.UsingProcessing.main(UsingProcessing.java:26)
Caused by: java.lang.IllegalAccessException: class processing.core.PApplet (in module core) cannot access class model.UsingProcessing (in module CovidSchoolSimulation) because module CovidSchoolSimulation does not export model to module core
    at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392)
    at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:489)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at core/processing.core.PApplet.runSketch(PApplet.java:10221)
    ... 3 more

The core.jar file is in the build path, so I don't understand why it doesn't work.


Solution

  • Do you have a module-info.java in you project? The error message you are seeing:

    java.lang.RuntimeException: java.lang.IllegalAccessException: class processing.core.PApplet (in module core) cannot access class model.UsingProcessing (in module CovidSchoolSimulation) because module CovidSchoolSimulation does not export model to module core

    means that the core (processing) module is not able to do reflection on your module. When you pass the class name in PApplet.main("model.UsingProcessing"); it uses reflection to instantiate your class. You need to do something like this in your module-info.java to allow the core module to use reflection on your module CovidSchoolSimulation or Test depending on which of you error messages is the correct one:

    module CovidSchoolSimulation { 
        opens CovidSchoolSimulation to core;
    }