eclipse 中的 java Instrumentation jar - manifest.mf 中失败

发布于 2024-11-14 02:02:08 字数 2098 浏览 4 评论 0原文

我正在尝试使用 java.lang.instrument.Instrumentation 类,该类需要使用“premain”类 - 可以在 此处堆栈

问题是我已经做到了这一点,但在另一个程序中使用它时遇到了问题。我的类看起来像这样:

public class InstrumentationWrapper {
  private static final String INSTR_KEY = "test.instrumentation";
  private static Instrumentation instrumentation;

  public static void premain(String options, Instrumentation inst) {
    Properties props = System.getProperties();
    if(props.get(INSTR_KEY) == null)
      props.put(INSTR_KEY, inst);
  }

  public static Instrumentation getInstrumentation() { 
    if (instrumentation == null) {
      instrumentation = (Instrumentation) System.getProperties().get(INSTR_KEY); 
    }

    return instrumentation;
  }

  public static long getObjectSize(Object o) {
      return instrumentation.getObjectSize(o);
  }

  public static long getSizeOfObjects (Collection<?> col) {
    long cumSize = 0;

    for (Object o : col) {
      cumSize = getObjectSize (o);
    }

    return cumSize;
  }
}

清单位于 Jar 文件中:

$ jar -tf target/instrumentator-1.0.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/testTools/
com/testTools/instrumentation/
com/testTools/instrumentation/InstrumentationWrapper.class
META-INF/maven/
META-INF/maven/com.netrecon.testTools/
META-INF/maven/com.netrecon.testTools/instrumentator/
META-INF/maven/com.netrecon.testTools/instrumentator/pom.xml
META-INF/maven/com.netrecon.testTools/instrumentator/pom.properties

并且 MANIFEST.MF 只是:

$ more src/resources/META-INF/MANIFEST.MF

Manifest-Version: 1.0

Premain-Class: com.testTools.instrumentation.InstrumentationWrapper

在 eclipse 的启动配置中,我遇到以下问题

Failed to find Premain-Class manifest attribute in Z:\ workspace\\testTools\instrumentor\target\instrumentator-1.0.jar

选项为-javaagent:${workspace_loc:instrumentator}\target\instrumentator-1.0.jar

我真的不确定如何让它工作 - 我真正需要做的就是拥有一个测试工具,让我看一下数组的内存占用情况。有什么想法吗?

I am trying to use the java.lang.instrument.Instrumentation class which requires usage of the 'premain' class - a good descrip can be found on stack here.

The problem is that I have done this and am having trouble using it in another program. My class looks like this:

public class InstrumentationWrapper {
  private static final String INSTR_KEY = "test.instrumentation";
  private static Instrumentation instrumentation;

  public static void premain(String options, Instrumentation inst) {
    Properties props = System.getProperties();
    if(props.get(INSTR_KEY) == null)
      props.put(INSTR_KEY, inst);
  }

  public static Instrumentation getInstrumentation() { 
    if (instrumentation == null) {
      instrumentation = (Instrumentation) System.getProperties().get(INSTR_KEY); 
    }

    return instrumentation;
  }

  public static long getObjectSize(Object o) {
      return instrumentation.getObjectSize(o);
  }

  public static long getSizeOfObjects (Collection<?> col) {
    long cumSize = 0;

    for (Object o : col) {
      cumSize = getObjectSize (o);
    }

    return cumSize;
  }
}

The manifest is in the Jar file as such:

$ jar -tf target/instrumentator-1.0.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/testTools/
com/testTools/instrumentation/
com/testTools/instrumentation/InstrumentationWrapper.class
META-INF/maven/
META-INF/maven/com.netrecon.testTools/
META-INF/maven/com.netrecon.testTools/instrumentator/
META-INF/maven/com.netrecon.testTools/instrumentator/pom.xml
META-INF/maven/com.netrecon.testTools/instrumentator/pom.properties

and the MANIFEST.MF is just:

$ more src/resources/META-INF/MANIFEST.MF

Manifest-Version: 1.0

Premain-Class: com.testTools.instrumentation.InstrumentationWrapper

In the launch configuration in eclipse I get the following problem

Failed to find Premain-Class manifest attribute in Z:\workspace\<project>\testTools\instrumentor\target\instrumentator-1.0.jar

and the option is -javaagent:${workspace_loc:instrumentator}\target\instrumentator-1.0.jar

I am really unsure how I can get this to work - All I really need to do is have a test harness that will let me look at the memory foot print of an array. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2024-11-21 02:02:08

我没有注意到什么,但如果您想进一步检查,您可以编写一个快速类来使用 java.util.jar.JarFile 打开您的 Jar 文件,并以编程方式检查清单。这将表明问题是否出在您编写清单的方式(可能是在错误位置的空格或其他什么地方)或它的加载方式(也许 premain 类的规范中存在拼写错误?)。

Nothing jumps out at me, but if you want to inspect further you can write a quick class to open up your Jar file with java.util.jar.JarFile and programatically inspect the manifest. This will indicate whether the issue is somehow in the way you wrote your manifest (maybe a space in the wrong place or something) or the way it's getting loaded (Maybe there is a typo in the specification of the premain class?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文