抛出java.lang.NoClassDefFoundError的Java-Sandbox示例

时间:2013-08-26 17:21:11

标签: java runtime-error sandbox

任何有使用Java-Sandbox经验的人,我已经实现了文档中的一个基本示例,但我无法使其正常工作。

代码: SandPlayground.java

import java.util.concurrent.TimeUnit;
import net.datenwerke.sandbox.*;
import net.datenwerke.sandbox.SandboxContext.AccessType;
import net.datenwerke.sandbox.SandboxContext.RuntimeMode;
import net.datenwerke.sandbox.SandboxedEnvironment;


public class SandPlayground {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("Running...");


        SandboxService sandboxService = SandboxServiceImpl.initLocalSandboxService();

        // configure context
        SandboxContext context = new SandboxContext();
        //context.setRunRemote(true);
        context.setRunInThread(true);
        context.setMaximumRunTime(2, TimeUnit.SECONDS, RuntimeMode.ABSOLUTE_TIME);
        context.addClassPermission(AccessType.PERMIT, "java.lang.System");
        context.addClassPermission(AccessType.PERMIT, "java.io.PrintStream");

        //run code in sandbox
        SandboxedCallResult<String> result = sandboxService.runSandboxed(MyEnvironment.class, context, "This is some value");

        // output result
        System.out.println(result.get());

    }

}

MyEnvironment.java

import net.datenwerke.sandbox.SandboxedEnvironment;
public class MyEnvironment implements SandboxedEnvironment<String> {

    private final String myValue;

    public MyEnvironment(String myValue){
       this.myValue = myValue;
    }

    @Override
    public String execute() throws Exception {
       /* run untrusted code */
       System.out.println(myValue);


       /* return some value */
       return "This is a different value";
    }
}

我收到了错误:

编辑:我已经包含了依赖项,但我仍然遇到了一些错误:

使用上面的代码我得到:

Exception in thread "main" net.datenwerke.sandbox.exception.SandboxedTaskKilledException: killed task as maxmimum runtime was exceeded
    at net.datenwerke.sandbox.SandboxMonitorDaemon.testRuntime(SandboxMonitorDaemon.java:82)
    at net.datenwerke.sandbox.SandboxMonitorDaemon.run(SandboxMonitorDaemon.java:57)
    at java.lang.Thread.run(Thread.java:724)

当我删除context.setMaximumRunTime()调用时,我得到:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/map/IdentityMap ...

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您很可能缺少javassist库(请参阅沙箱的文档介绍以获取依赖项:http://blog.datenwerke.net/p/the-java-sandbox.html)。您可以在sourceforge上找到javassist库:https://sourceforge.net/projects/jboss/files/Javassist/

javaassist库用于删除加载代码中的终结器。这可以在沙盒上下文中关闭:

contex.setRemoveFinalizers(false)

希望这有帮助。