Asce90237 / oj-code-sandbox

1 stars 0 forks source link

docker容器的java代码沙箱的一些问题 #1

Open daxiongxx opened 10 months ago

daxiongxx commented 10 months ago

请问为什么使用docker容器的java代码沙箱会报错,如下图 image 我写了一个单元测试来显示这个问题,代码如下:

package com.cq.sandbox.docker;

import com.cq.sandbox.core.JavaDockerCodeSandbox;
import com.cq.sandbox.model.ExecuteCodeRequest;
import com.cq.sandbox.model.enums.QuestionSubmitLanguageEnum;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.ArrayList;
@SpringBootTest
public class Mytest {
    @Autowired
    private JavaDockerCodeSandbox javaDockerCodeSandbox;
    @Test
    public void test(){
        ExecuteCodeRequest executeCodeRequest=new ExecuteCodeRequest();
        executeCodeRequest.setCode("import java.util.Scanner;\n" +
                "\n" +
                "public class Main {\n" +
                "\n" +
                "    public static void main(String[] args) {\n" +
                "        Scanner scanner = new Scanner(System.in);\n" +
                "        int a = scanner.nextInt();\n" +
                "        int b = scanner.nextInt();\n" +
                "        System.out.println( (a * b));\n" +
                "        scanner.close();\n" +
                "    }\n" +
                "}\n");
        executeCodeRequest.setLanguage(QuestionSubmitLanguageEnum.JAVA);
        ArrayList<String> arrayList=new ArrayList<>();
        arrayList.add("1 2");
        arrayList.add("2 3");
        arrayList.add("4 5");
        executeCodeRequest.setInputList(arrayList);
        javaDockerCodeSandbox.executeCode(executeCodeRequest);
    }

}

期待你能够回答我的问题,非常感谢!

Asce90237 commented 10 months ago

在DockerDao中executeStart方法内删除.withStdIn(inputStream) 试试

daxiongxx commented 10 months ago

在DockerDao中executeStart方法内删除.withStdIn(inputStream) 试试

好像不行,请问你之前是可以的吗,是不是docker要进行什么配置才可以的

Asce90237 commented 10 months ago

额,其实我没做出docker版的ACM模式沙箱,参数是从args中接收的

Asce90237 commented 10 months ago

你的code是ACM模式,所以不行

daxiongxx commented 10 months ago

你的code是ACM模式,所以不行

你的意思是,你的测试用例是在执行java命令的时候通过参数传过去的吗

Asce90237 commented 10 months ago

对,docker是这样的,我没做出来

daxiongxx commented 10 months ago

对,docker是这样的,我没做出来

好的,非常感谢!