java - "Failed to load Premain-Class manifest attribute"尝试使用 java 代理获取对象的大小时

标签 java instrumentation agent

当我尝试运行 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));
    }
}

MANIFEST.MF(对于 size.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);
    }
}

最佳答案

确保您已提供包含 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

最后一行是由于回车。

关于java - "Failed to load Premain-Class manifest attribute"尝试使用 java 代理获取对象的大小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2266525/

相关文章:

java - E/SQLite数据库 : Error inserting with SQLiteConstraintException

java - Android - 从静态类显示对话框

java - 可选地获取字段

java - OSGi 容器 KARAF 中的仪表

excel - AnyLogic:在模拟启动时用代理填充托盘架

git - TeamCity - 来自代理的 Git 推送

android - WSO2 MDM Android 代理问题

java - 确定 Java 中窗口激活事件的来源

java - 使用特定的类加载器从 `` byte[ ]`` 加载类

Android - ActivityMonitor getHits() 总是返回零