无法从jar文件中找到Premain-Class清单属性 [英] Failed to find Premain-Class manifest attribute from a jar file

查看:605
本文介绍了无法从jar文件中找到Premain-Class清单属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

MANIFEST.MD

Premain-Class : Agent

Agent.java

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

Test.java

public class Test {

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

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

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

,当我得到jar时,请按照以下步骤运行Test,然后对其进行编译

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

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

我想念什么吗?预先感谢

am I missing something ? thanks in advance

推荐答案

MANIFEST规范似乎不允许在之后使用空格,而只能在前添加空格.

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

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

所以这个

Premain-Class : Agent

应该成为

Premain-Class: Agent

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

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

Premain-Class: your.package.Agent

这篇关于无法从jar文件中找到Premain-Class清单属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆