I am using a GroovyClassLoader in my Java class to parse a certain (ideally complex) groovy file (to be executed in the next step):
Call in MyClass.java
final Class parsedClass = groovyClassLoader.parseClass(groovyFile);
Knowing that:
GroovyFile.groovy imports
import com.my.import.one.Import1DTO
import com.my.import.two.Import2DTO
import com.my.import.three.Import3DTO
import com.my.import.four.Import4DTO
import com.my.import.five.Import5DTO
When the parseClass method is invoked, this exeception raises:
Exceptions
unable to resolve class com.my.import.one.Import1DTO;
unable to resolve class com.my.import.two.Import2DTO;
unable to resolve class com.my.import.three.Import3DTO;
unable to resolve class com.my.import.four.Import4DTO;
unable to resolve class com.my.import.five.Import5DTO;
Can I obtain the behaviour I expect without parse every import class before parse the base class?
Thanks!
Here is an example MyClass.java which uses the addClasspath()
method on GroovyClassLoader
import groovy.lang.GroovyClassLoader;
public class MyClass {
public static void main(String... args) {
GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
// add "lib" to the classpath
groovyClassLoader.addClasspath("lib");
String groovyFile = "GroovyFile.groovy";
Class parsedClass = groovyClassLoader.parseClass(groovyFile);
System.out.println("class is " + parsedClass.toString());
}
}
I assume that the DTOs are written in Groovy and that we use "myimport", since "my.import.x" will fail due to illegal syntax. If we have a "lib" directory like so, with compiled classes:
lib/com/myimport/one/Import1DTO.groovy
lib/com/myimport/one/Import1DTO.class
lib/com/myimport/two/Import2DTO.groovy
lib/com/myimport/two/Import2DTO.class
and that GroovyFile.groovy exists in the main directory. eg
import com.myimport.one.Import1DTO
import com.myimport.two.Import2DTO
println "hi there"
then the above Java code works for me.
I am using Groovy 2.2.1 with groovy-all-2.2.1.jar on the classpath (for GroovyClassLoader).
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.