I'm writing a program using javassist to compile another Java class. The generated class use some objects like BigDecimal, List, ArrayList. So I import their packages:

ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(classDir); //classDir is my program Directory
pool.importPackage("java.util.List");
pool.importPackage("java.math.BigDecimal");
pool.importPackage("java.util.ArrayList");

Then I make some objects using CtField.make() for each object. When I use javassist to compile, it throws error:

CannotCompileException: [source error] no such class: BigDecimal

List is working fine, however, BigDecimal or ArrayList aren't. Is there any clue for this problem? Thanks!

有帮助吗?

解决方案 2

changing from new BigDecimal() by adding exact classPath as new java.math.BigDecimal() solves the problem!

其他提示

As the name implies, ClassPool.importPackage() (JavaDoc) is for importing packages, not classes. Considering that, you should use:

ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(classDir); //classDir is my program Directory
pool.importPackage("java.util");
pool.importPackage("java.math");

Note: starting from Javassist 3.14, it does support importing also fully-qualified-class-names. So with that version, your original code should also work.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top