首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Java 9上运行jol?

如何在Java 9上运行jol?
EN

Stack Overflow用户
提问于 2017-10-05 10:11:05
回答 1查看 2.7K关注 0票数 8

我正试图使用jol和Java9一起运行一个程序,但没有成功。

我在pom.xml中有下面的依赖项

代码语言:javascript
复制
<dependency>
    <groupId>org.openjdk.jol</groupId>
    <artifactId>jol-core</artifactId>
    <version>0.9</version>
</dependency>

这个程序很简单:

代码语言:javascript
复制
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());
    }
}

模块描述符:

代码语言:javascript
复制
module java9 {
    requires jol.core;
}

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

代码语言:javascript
复制
# WARNING: Unable to get Instrumentation. Dynamic Attach failed.
You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf

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

我可以从类路径成功地运行程序。不过,如何从模块路径运行它还是很有趣的。

EN

回答 1

Stack Overflow用户

发布于 2017-10-23 18:59:21

好的,我试过调试这一点--我发现警告的原因是InstrumentationSupport在应用程序启动时尝试了DYNAMIC_ATTACH。代码实际使用dynamicAttach的相关部分是

代码语言:javascript
复制
String name = "com.sun.tools.attach.VirtualMachine";
try {
    // JDK 9+ makes this class available on class path
    vmClass = ClassLoader.getSystemClassLoader().loadClass(name);
...

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

代码语言:javascript
复制
module java9 {    // module name 'joltrial' in output
    requires jol.core;
    requires jdk.attach;
} 

然后使用VM选项允许自我附加,如:-

代码语言:javascript
复制
-Djdk.attach.allowAttachSelf=true

在执行共享代码VM.current().details()时,输出应包括以下细节:

代码语言:javascript
复制
/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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46583083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档