0

任何有使用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());

    }

}

我的环境.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 ...

任何帮助深表感谢。

4

1 回答 1

1

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

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

contex.setRemoveFinalizers(false)

希望这可以帮助。

于 2013-08-26T18:56:02.573 回答