rahulk
rahulk

Reputation: 175

Getting "java.lang.NoClassDefFoundError" on running with javaagent

I am trying to instrument a jar file (main.jar) with javaagent.jar using BCEL. basically where ever I find any aload in bytecode, I m trying to insert a function call to a static function called Fun() in class "someclass" using

if (opcode instanceof aload) {
 iFactory.createInvoke("someclass", "fun", Type.VOID, new Type[]{}, Constants.INVOKESTATIC);

my "someclass" class reside in javaagent.jar

on executing

java  -javaagent:javaagent.jar -jar main.jar

or

java  -javaagent:javaagent.jar -jar main.jar javaagent.jar

or (I created a separate jar for my "someclass" called someclasscontained.jar)

java -cp someclasscontained.jar  -javaagent:javaagent.jar -jar main.jar 

I am getting

Exception in thread "main" java.lang.NoClassDefFoundError: someclass

error. i tried with -bootclasspath/p option but still not working. does anyone has any clue?

(1) all jar resides in same folder 2) this question may be similar to one question but solution is not correct/satisfactory for that question so please don't mark it duplicate )

Upvotes: 2

Views: 1877

Answers (1)

DenysK
DenysK

Reputation: 41

If someclass is defined in a different jar file, you need to add it to the Boot-Class-Path list in your agent jar manifest file:

some.jar  -> contains someclass.class
javaagent.jar -> contains your instrumenting classes and MANIFEST.MF file

MANIFEST.MF file should contain line like
Boot-Class-Path: javaagent.jar some.jar

Upvotes: 4

Related Questions