recompile source code in java method?
Currently want to work out some function like using Jpanel to edit source code and recompile it.
The basic ideal the basic java game I have with using Java.swing to perform. Instead of just showing the game, I will like to show part of the code with just pressing a button. The code will show out and let user to edit, save it with replace the original code.
The coming challenging function wanted to perform is ho开发者_运维技巧w can the system recompile with pressing one button in the Jpanel? I figure out there's difficulty when wanted to replace the original code and recompile.
Any idea in Java can perform such functionality? Will be appreciated on any comment and guidelines. Thanks
See JavaCompiler. Requires JDK on classpath.
To answer your question, you would call javac in your code, and write the output of the interpreter to a place where another part of your code would load it using a custom class loader.
To answer your meta-question, what's to prevent malicious users from entering destructive code? There's a reason games have a configuration file and possibly a scripting language. You want people to be able to modify certain parameters of the game, and not other parameters.
The technique you're looking for is call Hotswapping. You can accomplish that in the JVM using some of the debugging hooks as described, for example, here.
精彩评论