changing from new BigDecimal()
by adding exact classPath as new java.math.BigDecimal()
solves the problem!
javassist compilation error no such class
-
04-06-2022 - |
Question
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!
Solution 2
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow