compiling and running user code with JavaCompiler and ClassLoader

前端 未结 4 2012
悲&欢浪女
悲&欢浪女 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:21

    How can i protect my app from endless loop, and evil students ;)

    You cannot in one JVM. Evil students are particularly difficult to deal with because the smart ones will figure out some way to subvert your control mechanisms.

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

    No, unless you run it in a separate JVM.

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

    Yes there is, and there is nothing you can do about it (apart from separate JVMs). In fact, this would be a problem even if you could kill off student programs that get stuck in loops, etc. There are probably many ways that an application can cause the Java class libraries to leak memory / resources ... even after the application itself has finished and been GC'ed.

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

    Run each student application in a separate JVM that you launch from your server using Process and friends. You will need to write host operating system specific stuff to set execution time limits, and to kill of student applications that deadlock. Plus you've got all sorts of issues making sure that you don't accidentally trash the host machine performance by firing off too many JVMs.

    A better answer is to provide each student a desktop computer or a virtual machine and let them do their own thing.

提交回复
热议问题