【发布时间】:2011-06-08 08:23:44
【问题描述】:
我正在尝试使用需要使用“premain”类的 java.lang.instrument.Instrumentation 类 - 可以在 stack here 上找到一个很好的描述。
问题是我已经这样做了,但在另一个程序中使用它时遇到了问题。我的班级是这样的:
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 的启动配置中出现以下问题
在Z:\workspace\<project>\testTools\instrumentor\target\instrumentator-1.0.jar 中找不到 Premain-Class 清单属性
选项是-javaagent:${workspace_loc:instrumentator}\target\instrumentator-1.0.jar
我真的不确定如何让它工作 - 我真正需要做的就是拥有一个测试工具,它可以让我查看数组的内存足迹。有什么想法吗?
【问题讨论】:
标签: java jar manifest instrumentation