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!

Was it helpful?

Solution 2

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

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
scroll top