“无法加载 Premain-Class 清单属性”,同时尝试使用 java 代理获取对象的大小

2022-09-02 01:50:08

当我尝试运行Java程序()时,我得到以下错误:java -javaagent:size.jar ObjectSizeTest

Failed to load Premain-Class manifest attribute from D:\workspace\ObjectSizeTest\size.jar
Error occurred during initialization of VM
agent library failed to init: instrument

以下是 ObjectSizeTest 的代码:

public class ObjectSizeTest {
    public static void main(String[] args) {
        String s = new String("sai");
        System.out.println(ObjectSizeFetcher.getObjectSize(s));
    }
}

清单。MF(用于尺寸.jar):

Manifest-Version: 1.0
Created-By: 1.5.0_18 (Sun Microsystems Inc.)

Premain-Class: ObjectSizeFetcher

这是ObjectSizeFetcher的代码:

import java.lang.instrument.Instrumentation;

public class ObjectSizeFetcher {
    private static Instrumentation instrumentation;

    public static void premain(String args, Instrumentation inst) {
        instrumentation = inst;
    }

    public static long getObjectSize(Object o) {
        return instrumentation.getObjectSize(o);
    }
}

答案 1

确保已提供包含 pre-main 方法的类的完整 java 路径。例如,像这样的 org.eclipse.anotherpckg.ObjectSizeFetcher。其次,在名称之前必须有一个空格,并在末尾返回回车。例如

Manifest-Version: 1.0
Created-By: 1.5.0_18 (Sun Microsystems Inc.)
Premain-Class: org.eclipse.package.ObjectSizeFetcher

最后一行是由于回车。


答案 2

您应该在清单中添加。中频:

Premain-Class: org.your.package.ObjectSizeFetcher+ 新线

相反

Premain-Class: ObjectSizeFetcher


推荐