compiling and running user code with JavaCompiler and ClassLoader

前端 未结 4 2010
悲&欢浪女
悲&欢浪女 2020-12-09 07:01

I am writing web app for java learning. Using which users may compile their code on my serwer + run that code. Compiling is easy with JavaCompiler:

    Jav         


        
4条回答
  •  無奈伤痛
    2020-12-09 07:12

    is there any way to run that code with a lifetime ?

    Create a process which monitors child processes and terminates it if it takes too long.

    is there any risk with memory leaks, and what can i do to fix this.

    You should be able to do that, to some extent, by controlling how much memory is allocated (like the -Xmx parameter to Sun's JVM).

    is this good solution, or can you suggest something better ?

    I'm not sure a solution has been proposed, but here's a thought. Install a SecurityManager that greatly restricts what the executed code can do, such as access the filesystem, spawn processes, etc. Combine that with a process that monitors for timeouts, limits the allocated memory, runs the application under a separate user account, etc., and I think you can have something workable.

    What you're looking for is possible, but may not be entirely so if your restricted to only Java.

提交回复
热议问题