Search code examples
javacommand-linejarjavaagents

java agent class not found exception


I have a directory structure "D:\workspace 2\project\lib" where I have all the files "AgentMain.java"(which I copied from src and placed it here to make it easier), "asm-all.3.3.jar", "myagent.jar". Now when I try to invoke my java program like this from command prompt

D:\workspace 2\project\lib>java -javaagent:myagent.jar -cp asm-all-3.3.jar;. AgentMain.java

it gives me exception in thread main java.lang.NoclassDefFoundError AgentMain/java and says couldnot find the main class AgentMain.java

What is wrong here?


Solution

  • You're trying to execute your source file (AgentMain.java) instead of the class. Make sure you've compiled AgentMain with javac and then remove the .java file extension.

    UPDATE based on discussion from the comments: Since your AgentMain class is defined in a package called "main" once you compile your class, its corresponding class file must be in a directory called "main".

    so if you're running from your "d:\workspace 2\project\lib" directory with the command you included, the compiled class needs to be in d:\workspace 2\project\lib\main\AgentMain.class