Search code examples
javaclassnotfoundexceptionmanifest.mfjavaagents

Javaagent gives ClassNotFoundException


I am trying to use a javaagent called LoggerAgent to get details of log some Java applications. But I am getting a ClassNotDefinedException:

Exception in thread "main" java.lang.ClassNotFoundException: com.LoggerAgent
    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)
    at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304)
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
FATAL ERROR in native method: processing of -javaagent failed
Aborted

Interestingly, if I run the javaagent on the same LoggerAgent class (I have intentionally defined a main function in it to test this), then the program works normally. In case it is necessary, I am pasting the manifest file of the javaagent (named as agent.jar) here:

Premain-Class: com.LoggerAgent
Class-Path: lib/javassist.jar
Agent-Class: com.LoggerAgent
Boot-Class-Path: agent.jar

Any ideas about what is going wrong here?


Solution

  • Compare the classpath for the main class (the one you wrote intentionally) and the classpath for the class in which you are getting the exception.

    The Class-Path in the manifest is relative to the executable JAR the way you've written it.