سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

You cannot run .java files, you need to compile them first with javac and then run the compiled code (.class files). In your example, you have to run

javac -cp asm-all-3.3.jar;. AgentMain.java

and then

java -javaagent:myagent.jar -cp asm-all-3.3.jar;. AgentMain

You should be referencing the compiled .class file, not the .java file

java does not interpret source files.

You have to compile AgentMain.java with javac first. Then you have to specify AgentMain on the command line instead of AgentMain.java.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top