我想计算我创建的对象(在名为TreapDS的程序包中重新捕获)的内存使用情况。我发现我需要遵循基于this page的这些步骤。我总是使用Eclipse,所以我对使用命令运行Java代码不熟悉。我问了一个问题,如何在this page中基于该页面构建jar文件,但未收到任何答案。这是我的包的层次结构:

Indexing
| --- bin
|---- MemoryUsage
              | -- mainfest.MF
              |  -- Myagent
| --- src
       |--- treapDS
       |      |--- Treap
       |---- MemoryUsage
              | -- TestCase
              |  -- Myagent


我可以基于在此地址中运行命令来创建一个jar文件

Indexing/bin$ jar -cmf MemoryUsage/manifest.MF agent.jar MemoryUsage/MyAgent.class


它在bin文件夹中创建了agent.jar,当我提取这个jar文件时,它包含两个文件夹MemoryUsage,其中包含Myagent.class和文件夹META-INF,其中包含MANIFEST.MF

但是,当我运行以下命令时,出现了异常,并且我不知道如何通过提及Treap的地址来解决它。有什么办法可以用日食做到这一点?

/Indexing/bin$ java -javaagent:agent.jar -cp MemoryUsage/TestCase

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:386)
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
Caused by: java.lang.NoClassDefFoundError: treapDS/Treap
    at MemoryUsage.MyAgent.premain(MyAgent.java:9)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: treapDS.Treap
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more
FATAL ERROR in native method: processing of -javaagent failed
Aborted (core dumped)


我阅读了这些页面1 2,但无法解决我的问题。

最佳答案

这是因为treapDSTreapDS不同。类名区分大小写!堆栈跟踪非常清楚:类加载器正在尝试加载treapDS.Treap而不是TreapDS.Treap,而前者不存在。

修复您的源代码,然后重新编译以在所有位置具有正确的区分大小写的拼写,并且该拼写应该消失。

大概是第一次“一切都编译”的原因是因为您在没有适当大小写敏感文件路径的OS上进行开发。

关于java - 使用javaagent命令时解决java.lang.NoClassDefFoundError异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34834903/

10-10 05:50