使用JavaCompiler和ClassLoader编译和运行用户代码 [英] compiling and running user code with JavaCompiler and ClassLoader

查看:120
本文介绍了使用JavaCompiler和ClassLoader编译和运行用户代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为java学习编写web应用程序。使用哪些用户可以在我的服务器上编译他们的代码+运行该代码。
JavaCompiler编译很简单:

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:

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, prepareFile(nazwa, content));

    task.call();

    List<String> returnErrors = new ArrayList<String>();
    String tmp = new String();
    for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
        tmp = String.valueOf(diagnostic.getLineNumber());
        tmp += " msg: " + diagnostic.getMessage(null);
        returnErrors.add(tmp.replaceAll("\n", " "));
    }

我设法用代码加载类:

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager manager = compiler.getStandardFileManager(null, null, null);

    try {
        URL[] urls = {new URL("file:///root/"), new URL("file://C:\\serv\\Apache Tomcat 6.0.20\\bin\\")};
        ClassLoader cl_old = Thread.currentThread().getContextClassLoader();
        ClassLoader cl_new = new URLClassLoader(urls, cl_old);
        Class compiledClass = cl_new.loadClass(CLASS_NAME);
        Method myMethod = compiledClass.getMethod(METHOD_NAME);
        Object tmp = myMethod.invoke(null);
    } catch (Exception ex) {
        Logger.getLogger(ITaskCompile.class.getName()).log(Level.SEVERE, null, ex);
    }

我如何保护我的应用程序免受无限循环和邪恶学生的影响;)

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


  1. 有没有办法在一生中运行该代码?

  2. 内存有任何风险泄漏,我该怎么做才能解决这个问题。

  3. 这是一个很好的解决方案,还是你可以提出更好的建议?

thx。
Tzim

thx. Tzim

推荐答案


我如何保护我的应用程序免受无限循环和邪恶的学生;)

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

您不能在一个JVM中。邪恶的学生特别难以应对,因为聪明的学生会想出一些方法来颠覆你的控制机制。

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)有没有使用生命周期运行该代码的方法?

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

否,除非您在单独的JVM中运行它。

No, unless you run it in a separate JVM.


2)是否有任何内存泄漏的风险,我该怎么做才能解决这个问题。

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

是的,没有什么可以做的(除了单独的JVM)。实际上,即使您可以杀死陷入循环等的学生程序,这也是一个问题。应用程序可能有很多方法导致Java类库泄漏内存/资源......即使在应用程序之后本身已经完成并且已经完成了GC。

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)这是一个很好的解决方案,还是你可以提出更好的建议?

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

使用 Process 和朋友在您的服务器上启动的单独JVM中运行每个学生应用程序。您将需要编写主机操作系统特定的东西来设置执行时间限制,并杀死死锁的学生应用程序。此外,您还有各种各样的问题,确保您不会通过触发太多JVM来意外地破坏主机性能。

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.

这篇关于使用JavaCompiler和ClassLoader编译和运行用户代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆