【问题标题】:How to run jol on Java 9?如何在 Java 9 上运行 jol?
【发布时间】:2018-03-16 22:08:51
【问题描述】:

我正在尝试使用 jol 和 Java 9 运行程序,但没有成功。

我在pom.xml 中有以下依赖:

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

程序很简单:

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 java9 {
    requires jol.core;
}

当我从 IDEA 运行程序时,我看到以下输出:

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

我在 IDEA 的 VM 参数中添加了 -Djdk.attach.allowAttachSelf=true,但它没有帮助(仍然是相同的输出)。

附: 我可以从类路径成功运行程序。尽管如此,如何从模块路径运行它还是很有趣的。

【问题讨论】:

  • 它可能依赖于反映已被模块系统关闭的 JDK 内部。按照this尝试--add-opens java.base/java.util=ALL-UNNAMED
  • @Michael 感谢您链接到我的问题 :) 我不认为这是相关的...这已在 0.7
  • 您能否更新问题以明确它是否作为命令行上的模块工作?正如目前所写的那样,当 JOL 在类路径上时,您在命令行上成功运行它,但在 IDE 中尝试将其作为模块进行,对吗?
  • @AlanBateman 如果 jol-core.jar 和 my-program.jar 都在类路径上,它运行良好。当它们在模块路径上时它会失败。
  • 在这种情况下,关于使用 JDK 内部的其他评论可能是有效的,JDK 不会向所有模块打开任何包,它只是(并且只是暂时)只有它的包在类路径。使用 -Dsun.reflect.debugModuleAccessChecks=true 运行可能会发现一些问题,否则将其带到 jol-dev 讨论。

标签: java java-9 jol


【解决方案1】:

好吧,我tried debugging 这一点发现警告的原因是InstrumentationSupport 在应用程序启动时尝试DYNAMIC_ATTACHdynamicAttach 的相关部分,其中代码实际使用了 VirtualMachine

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

该类没有加载最初解析的模块,因为它的包是从模块描述符中当前缺失的jdk.attach 模块导出的。因此,您可以更新为使用模块声明:

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

然后允许使用 VM 选项进一步自连接:-

-Djdk.attach.allowAttachSelf=true

在执行共享代码 [VM.current().details()] 时,输出应包括详细信息 -

/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

【讨论】:

  • 谢谢。我添加了requires jdk.attach。不幸的是,我仍然无法让我的应用程序运行。它只是挂起,没有任何输出。有趣的是,在调试模式下(在 IDEA 中)一切都很好
  • @ZhekaKozlov 无法运行应用程序,是否意味着无法打印相关代码中提到的 VM 详细信息?因为我在 IDEA 中使用了相同的代码来让它运行。如果这可能很重要,我正在使用 IntelliJ IDEA 的 2017.3 EAP。
  • 是的,它启动但不打印任何内容。在那之后我必须杀死我的应用程序。
  • agentProcess.waitFor() ... 如有必要,使当前线程等待,直到此 {@code Process} 对象表示的进程终止。 ...当您调试 senseAccess 中的代码时,args 是什么?此外,是否有任何其他进程已经为您运行,可能会导致当前等待?
  • 我将此报告给jol 邮件列表:mail.openjdk.java.net/pipermail/jol-dev/2017-October/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 1970-01-01
  • 2019-03-08
  • 2018-02-26
  • 2016-01-26
  • 1970-01-01
  • 2018-03-12
相关资源
最近更新 更多