Reputation: 3568
I was wondering about the possibility to send Java Code to a servlet that the servlet will take and execute it?
For example I would send a chunk of code as String
, which will be added and executed. Can anybody think of a way of how I would approach this?
Upvotes: 1
Views: 460
Reputation: 42597
This is certainly possible - you could send the compiled bytecode Base64-encoded, then decode it at the servlet and use a classloader to load it.
Or you could send a jarfile similarly encoded, save it to disk and add it to a custom URLClassLoader.
You may have major security issues if the code is untrusted, though.
See What are the security risks I should guard against when running user-supplied Java code?
Upvotes: 1
Reputation: 22847
tools.jar from JDK contains classes to compile Java code (used by Ant etc.), you can also use Eclipse compiler, if you want. It is possible, but from security point of view (code injection etc.) it would be disaster ;)
Upvotes: 0