无法从jar文件中找到Premain-Class清单属性

时间:2019-03-20 10:53:44

标签: java jar manifest instrumentation

我从单个.class创建了一个jar,并且在清单Premain-class中提到了,该jar是按预期生成的,但是当我尝试运行使用该jar中的类的程序时,出现错误< / p>

MANIFEST.MD

Premain-Class : Agent

Agent.java

import java.lang.instrument.Instrumentation;
public class Agent{
    private static Instrumentation inst;
    public static void premain(String paramString, Instrumentation paramInstrumentation) 
    { 
        inst = paramInstrumentation;
    }

    public static long size(Object paramObject)
    {
        return inst.getObjectSize(paramObject);
    }
}

Test.java

public class Test {

  public static void main (String[] args){
    System.out.println(Agent.size(Integer.valueOf(9)));
  }
}

当我获取Agent.class时,在MANIFEST.MD文件所在的文件夹中,我执行以下命令

jar -cvfm agent.jar MANIFEST.MF *.class

当我获得jar时,按照以下步骤运行测试,将其编译后即可

java -javaagent:agent.jar Test

我收到以下错误

Failed to find Premain-Class manifest attribute in agent.jar
Error occurred during initialization of VM
agent library failed to init: instrument
我想念什么吗?预先感谢

1 个答案:

答案 0 :(得分:0)

MANIFEST规范似乎不允许在后加空格,而只能在前加空格。

header: alphanum *headerchar ":" SPACE *otherchar newline
          *continuation

所以这个

Premain-Class : Agent

应该成为

Premain-Class: Agent

另外,将您的Agent放在一个包装下,不要使用默认的包装

Premain-Class: your.package.Agent