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. e.g.
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).