Is there a way to compile and load a Java class during runtime without creating and storing a file in an operating system filesystem?
Say I have a database record that contains the string representation of a java class. I pull that string into Java memory. My goal is to compile that String into a java class and then load that class.
Let me make this clear. I want nothing to do with .java files or .class files, in any operating system.
Is this possible? And how?
For instance, here is code to load a Groovy class at runtime, and invoke a method:
ClassLoader parent = getClass().getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(new File("src/test/groovy/script/HelloWorld.groovy"));
// let's call some method on an instance
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
Object[] args = {};
groovyObject.invokeMethod("run", args);
but could we do something like:
ClassLoader parent = getClass().getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(new String("public class GroovyClass{}"));
?