jacoco / jacoco/jacoco

IllegalAccessError when executing Java 9 modules after offline instrumentation

Open
#545 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

component: core
Dominant language
Java
Stars
4.6k
Forks
1.2k
Avg merge
1d 9h
Merged PRs (30d)
23

Description

$ mkdir -p src/org/example

$ cat <<END > src/module-info.java
module org.example {
}
END

$ cat <<END > src/org/example/HelloWorld.java
package org.example;

class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, World");
  }
}
END

$ javac -d mods/org.example \
        src/module-info.java \
        src/org/example/HelloWorld.java
$ java -jar jacococli.jar \
       instrument mods/org.example/org \
       --dest mods-instrumented/org.example/org
$ java \
       -cp mods-instrumented/org.example:jacocoagent.jar \
       org.example.HelloWorld
Hello, World!
$ cp mods/org.example/module-info.class mods-instrumented/org.example

$ java --version
java 9-ea
Java(TM) SE Runtime Environment (build 9-ea+171)
Java HotSpot(TM) 64-Bit Server VM (build 9-ea+171, mixed mode)

$ java \
       --module-path mods-instrumented \
       -cp jacocoagent.jar \
       --module org.example/org.example.HelloWorld

leads to

Exception in thread "main" java.lang.IllegalAccessError: class org.example.HelloWorld (in module org.example) cannot access class org.jacoco.agent.rt.internal_c42d03f.Offline (in unnamed module @0x15975490) because module org.example does not read unnamed module @0x15975490
	at org.example/org.example.HelloWorld.$jacocoInit(HelloWorld.java)
	at org.example/org.example.HelloWorld.main(HelloWorld.java)

as well as

$ java \
       --module-path mods-instrumented \
       -javaagent:jacocoagent.jar=excludes=org.example.HelloWorld \
       --module org.example/org.example.HelloWorld

According to http://openjdk.java.net/projects/jigsaw/

The module system was merged into JDK 9 build 111 in March 2016

and this error is reproducible with all builds down to 111.

Workaround

Use --add-reads:

$ java \
       --module-path mods-instrumented \
       -cp jacocoagent.jar \
       --add-reads org.example=ALL-UNNAMED \
       --module org.example/org.example.HelloWorld
$ java \
       --module-path mods-instrumented:jacocoagent.jar \
       --add-modules jacocoagent \
       --add-reads org.example=jacocoagent \
       --module org.example/org.example.HelloWorld

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the module-info.java and HelloWorld.java reproduction, then run the jacococli.jar offline instrumentation and modular java commands shown in the issue. Compare the failing offline and javaagent paths, including the --add-reads workaround. Done means the instrumented Java 9 module runs without IllegalAccessError or requiring that workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.