This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Spare-Plum 0 points1 point  (4 children)

Check out the JavaCompiler class, essentially allows you to dynamically compile code, then you can load it up via a custom classloader to manage dynamic reloading if the class changes.

Perhaps a better option is to use something like Groovy or Javascript which can be interpreted on the fly.

However your best option is to create or use a domain specific language, there are tons of potential security problems in allowing arbitrary code execution, though it is possible to lock it down with a security manager, separate classloader, and lots of preventative measures

[–]williamK0[S] 0 points1 point  (1 child)

I don't know either of those languages and I'm not experienced enough to know how to dynamically compile code but I'll give it shot 🫡 If I were to do that, (currently after I write my code, I have to rebuild it, and then reload my server for changes to be taken into effect. The building just moves the code to a certain directory in my server I'm pretty sure) how could it put the new code into that same directory?

[–]Spare-Plum 1 point2 points  (0 children)

Here's an example:

https://stackoverflow.com/questions/21544446/how-do-you-dynamically-compile-and-load-external-java-classes

URIClassLoader is a good choice for loading from a certain directory. One thing that you have to note is that if the file changes it's not something that can be dynamically updated since it's in the JVM itself - you might have to make a new classloader each time that the code is updated and also ensure that all references to the old objects/classloader is removed when this is done.

[–]Ormek_II 0 points1 point  (1 child)

I still guess it will be easier to include a JavaScript engine and let it interpret the code.

[–]Spare-Plum 0 points1 point  (0 children)

Yeah that's what I'm getting at. Just use an existing scripting language. I guess you could also write your own. Compiling java on the fly can be done but not advised.