Failed to find Premain-Class manifest attribute from a jar file

余生长醉 提交于 2021-02-10 14:11:43

问题


I created a jar from a single .class and I mentioned in the manifest the Premain-class, the jar was generated as expected but when I try to run a program that uses the class from that jar , I get an error

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

When I get the Agent.class, in the same folder where MANIFEST.MD file exist I execute the following command

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

and when I get the the jar I run the Test, after compiling it, as follow

java -javaagent:agent.jar Test

and I get the following error

Failed to find Premain-Class manifest attribute in agent.jar
Error occurred during initialization of VM
agent library failed to init: instrument

am I missing something ? thanks in advance


回答1:


The MANIFEST specification doesn't seem to allow spaces after the key, but only before the value.

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

So this

Premain-Class : Agent

should become

Premain-Class: Agent

Also, put your Agent under a package, don't use the default one

Premain-Class: your.package.Agent


来源:https://stackoverflow.com/questions/55259056/failed-to-find-premain-class-manifest-attribute-from-a-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!