简体   繁体   中英

javassist get CtClass from a third party library/jar

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM