Java - let user write own code, then compile it, then use it within same runtime
I need to perform following steps:
- Let user write own code - no problem, it's just one interface to implement and I save a file
- Compile it - no problem, I used
ToolProvider.getSystemJavaCompiler()
and created .class file - Let user use this new code - h开发者_Python百科ere I am stuck. I have .class file and now what? I need somehow add it in my project and I don't know how.
Thanks for help!
Use a URLClassLoader
to load the classes. Then you can use reflection to instantiate and manipulate them.
What you need to do is write your own ClassLoader that will load the classes you have just compiled.
There are many examples on the web on how to do that.
Here is one to load from the web: http://kazi-masudul-alam.blogspot.com/2008/01/java-classloader.html
精彩评论