I know this is old, but I wanted to add to the stream:
Yes it is possible. Basically you use the JavaCompiler class in java to compile any string at runtime and return an instance of java.lang.Class that can be used to instantiate the object.
I wrote a small minimal library for the project I'm currently working on. Feel free to use the library as you wish. EzReflection allows you to fully compile code in memory without requiring you to write a .class file.
Using ezReflections, the code would look like this:
InMemoryEzReflectionsCompiler compiler = new InMemoryEzReflectionsCompiler(); String src = new Scanner(new File("filename")).useDelimiter("\\Z").next(); Class<?> cls = compiler.compileClass("name of class", src);
You can also find a set of tutorials on the Github page on how to use ezReflections.
source share