简体   繁体   中英

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 how 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM