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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2266525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:26:59  来源:igfitidea点击:

"Failed to load Premain-Class manifest attribute" while trying to get the size of an object using java agent

javainstrumentationagent

提问by java_geek

When i try to run a java program (java -javaagent:size.jar ObjectSizeTest) i get the following error:

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

Here is ObjectSizeTest's code:

这是 ObjectSizeTest 的代码:

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

MANIFEST.MF (for size.jar):

MANIFEST.MF(用于 size.jar):

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

Premain-Class: ObjectSizeFetcher

and here is ObjectSizeFetcher's code:

这是 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);
    }
}

回答by user758867

Make sure you have give full java path of the class containing the pre-main method. for example like this org.eclipse.anotherpckg.ObjectSizeFetcher. Secondly there must be a space before the name and carriage return at the end. for example

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

The last line is due to carriage return.

最后一行是由于回车。

回答by maruthy

It is an issue with the jar command itself. jar command must be used with cfm attributes, to include customized MANIFEST.MF, otherwise jar will create one file and insert its own contents which do not include the PreMain-Class attribute as we mention in customized manifest.mf file.

这是 jar 命令本身的问题。jar 命令必须与 cfm 属性一起使用,以包含自定义的 MANIFEST.MF,否则 jar 将创建一个文件并插入自己的内容,这些内容不包含我们在自定义 manifest.mf 文件中提到的 PreMain-Class 属性。

回答by Max Gabderakhmanov

You should add in MANIFEST.MF:

您应该在 MANIFEST.MF 中添加:

Premain-Class: org.your.package.ObjectSizeFetcher+ new line

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

insted

插入

Premain-Class: ObjectSizeFetcher

Premain-Class: ObjectSizeFetcher

回答by Hymanson

just run java size.jar ObjectSizeTestthe problem is caused by java agent it has a tranformer class.

只是运行java size.jar ObjectSizeTest问题是由java代理引起的,它有一个变压器类。