Question

I want to add with javassist a SLF4J logger in my class. So first, I try to get its CtClass such as I can build the field after:

CtClass loggerClass = pool.get(org.slf4j.Logger.class.getName());

But I never pass that line and always get a javassist.NotFoundException.

I tried different things:

pool.importPackage("org.slf4j.Logger");
// or
pool.importPackage("org.slf4j");

And I even tried to pass org.slf4j.Logger to the URLClassLoader:

// add org.slf4j.Logger to the list of urls...
// then:
URLClassLoader loader = new URLClassLoader(urls);
ClassPool.getDefault().insertClassPath(new LoaderClassPath(loader));

But nothing works.

Note: This is how pool is created just after the call to insertClassPath:

final ClassPool pool = new ClassPool(ClassPool.getDefault());
pool.childFirstLookup = true;

I'm completely stuck here. Any help would be much appreciated.

Was it helpful?

Solution

Provide the path upto the jar file name.
Check below code. I coud able to see the Logger class loaded of slf4j

ClassPool pool = new ClassPool(ClassPool.getDefault());
pool.appendClassPath("./otherlib/slf4j-api-1.7.6.jar");
CtClass ctClass = pool.get("org.slf4j.Logger");
System.out.println(ctClass);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top