Xorty
Xorty

Reputation: 18861

Java - let user write own code, then compile it, then use it within same runtime

I need to perform following steps:

  1. Let user write own code - no problem, it's just one interface to implement and I save a file
  2. Compile it - no problem, I used ToolProvider.getSystemJavaCompiler() and created .class file
  3. Let user use this new code - here 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!

Upvotes: 5

Views: 177

Answers (2)

Romain Hippeau
Romain Hippeau

Reputation: 24375

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

Upvotes: 1

Bozho
Bozho

Reputation: 597046

Use a URLClassLoader to load the classes. Then you can use reflection to instantiate and manipulate them.

Upvotes: 5

Related Questions