当我尝试运行一个Java程序(java -javaagent:size.jar ObjectSizeTest
)时,我会得到以下错误:
Failed to load Premain-Class manifest attribute from D:\workspace\ObjectSizeTest\size.jar
Error occurred during initialization of VM
agent library failed to init: instrument
这里是 ObjectSizeTest 的代码:
public class ObjectSizeTest {
public static void main(String[] args) {
String s = new String("sai");
System.out.println(ObjectSizeFetcher.getObjectSize(s));
}
}
MANIFEST.MF (用于 size.jar):
Manifest-Version: 1.0
Created-By: 1.5.0_18 (Sun Microsystems Inc.)
Premain-Class: ObjectSizeFetcher
这里是ObjectSizeFetcher的代码:
import java.lang.instrument.Instrumentation;
public class ObjectSizeFetcher {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}
public static long getObjectSize(Object o) {
return instrumentation.getObjectSize(o);
}
}
ObjectSizeFetcher
类的源代码。 - rochb