Cannot get -javaagent to run without error

DungeonTiger picture DungeonTiger · Mar 26, 2012 · Viewed 8.1k times · Source

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.

Answer

DungeonTiger picture DungeonTiger · Mar 27, 2012

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.