How to run jol on Java 9?

4k views Asked by At

I'm trying to run a program using jol with Java 9 but with no luck.

I have the following dependency in pom.xml:

<dependency>
    <groupId>org.openjdk.jol</groupId>
    <artifactId>jol-core</artifactId>
    <version>0.9</version>
</dependency>

The program is simple:

package org.example;

import org.openjdk.jol.vm.VM;

public class Example {
    public static void main(String[] args) throws Throwable {
        System.out.println(VM.current().details());
    }
}

Module descriptor:

module java9 {
    requires jol.core;
}

When I run the program from IDEA, I see the following output:

# WARNING: Unable to get Instrumentation. Dynamic Attach failed.
You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf

I added -Djdk.attach.allowAttachSelf=true to the VM arguments in IDEA but it didn't help (still the same output).

P.S. I can run the program successfully from the classpath. Still, it is interesting how to run it from the module path.

1

There are 1 answers

12
Naman On

Well, I tried debugging this a little further an found that the cause of the warning is InstrumentationSupport trying DYNAMIC_ATTACH on the application startup. The relevant section of dynamicAttach where the code actually makes use of the VirtualMachine is

String name = "com.sun.tools.attach.VirtualMachine";
try {
    // JDK 9+ makes this class available on class path
    vmClass = ClassLoader.getSystemClassLoader().loadClass(name);
...

The class is not loaded with initially resolved modules since its package is exported from jdk.attach module which is currently missing from the module descriptor. Hence you can update to using the module declarations as :

module java9 {    // module name 'joltrial' in output
    requires jol.core;
    requires jdk.attach;
} 

and then allow self-attach further using the VM option as:-

-Djdk.attach.allowAttachSelf=true

On executing the shared code [VM.current().details()], the output shall include details as -

/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin/java -Djdk.attach.allowAttachSelf=true "-javaagent:/Applications/IntelliJ IDEA 2017.3 CE EAP.app/Contents/lib/idea_rt.jar=53783:/Applications/IntelliJ IDEA 2017.3 CE EAP.app/Contents/bin" -Dfile.encoding=UTF-8 -p .../joltrial/target/classes:.../.m2/repository/org/openjdk/jol/jol-core/0.9/jol-core-0.9.jar -m joltrial/com.Sample
# WARNING: Unable to attach Serviceability Agent. Unable to attach even with module exceptions: [org.openjdk.jol.vm.sa.SASupportException: Sense failed., org.openjdk.jol.vm.sa.SASupportException: Sense failed., org.openjdk.jol.vm.sa.SASupportException: Sense failed.]
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit shift.
# WARNING | Compressed references base/shifts are guessed by the experiment!
# WARNING | Therefore, computed addresses are just guesses, and ARE NOT RELIABLE.
# WARNING | Make sure to attach Serviceability Agent to get the reliable addresses.
# Objects are 8 bytes aligned.
# Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

Process finished with exit code 0