3

I'm trying to create a simple Java Agent program before I implement the real thing. I cannot get it to run. Clearly I have some sort of configuration or class path problem. No amount of looking and trying has lead to me the problem.

If I run:

java -cp ./demoAgent.jar -javaagent:./demoAgent.jar com.kingtigerbooks.demoMod.Main

I get the following error:

Exception in thread "main" java.lang.ClassNotFoundException: com.kingtigerbooks.demoAgent.Agent
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:280)
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:338)
FATAL ERROR in native method: processing of -javaagent failed
Abort trap

I'm running this on my Mac. The jar file with the agent is in the current directory. The manifest for the jar file looks like this:

Manifest-Version: 1.0
Premain-class: com.kingtigerbooks.demoAgent.Agent
Can-Redefine-Classes: true

If smells like a class path problem, but as you can see I have included the jar on the class path. Any help would be appreciated. Its a very simple project.

2
  • Something is clearly odd here. I did more or less the same thing on Ubuntu and it worked. I don't see what's different. Ideally what I would like are hints on how to debug this type of issue. Mar 27, 2012 at 0:02
  • I got this error my compiling my project with jdk1.8 when I should have been using 1.7....
    – djangofan
    Jan 30, 2015 at 0:48

2 Answers 2

4

Problem solved. I had a bad jar file. The agent jar file did not contain the agent class. By building the correct jar file and putting the fully qualified path to the jar on in the javaagent parameter it all works.

2

when you run an agent, you do not add the agent jar to the normal classpath.

as a side note, you can add -verbose:class to the command line to get info about the class loading process.

5
  • Unfortunately this resulted in the same error. The command line was java -javaagent:./demoAgent.jar com.kingtigerbooks.demoMod.Main Mar 26, 2012 at 19:49
  • @DungeonTiger - do you have the Main class in your classpath somewhere?
    – jtahlborn
    Mar 26, 2012 at 20:47
  • Hummm....I would say yes, since if I remove the -javaagent option, it runs and I get my beautiful Hello World type output. Yeh. So that tells me this part is correct. Also, its not complaining about the Main class its complaining about the Agent class which is in the jar, demoAgent.jar. Mar 26, 2012 at 21:41
  • @DungeonTiger - do you have the CLASSPATH set? also, what is the directory structure under the dir where you are running the command?
    – jtahlborn
    Mar 27, 2012 at 0:43
  • You're going to shoot me. Found the problem. I had put the wrong class file in the jar. Doh. I did learn from you that I don't need to put the jar on the class path if it is listed as part of the javaagent option. Thanks for your help. Now on to cooler things which are using BCEL to modify the code as it loads. Yeh! Mar 27, 2012 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.