This project is a code playground that allows users to submit Java code, which is then executed in a secure, sandboxed environment using Docker containers. The primary goal is to execute Java code safely and efficiently while providing feedback on the code execution status, output, and errors.
- Execute Java code in a secure Docker container
- Validate and format submitted code before execution
- Check for unsafe or potentially malicious code
- Returns execution results (success, failure, or error message)
- Java 17 (or newer)
- Spring Boot for the backend framework
- Docker for sandboxed code execution
- Maven for dependency management
- JUnit and Mockito for testing
- Docker Image Setup: Ensure that Docker is running on your machine.
- Build the project: Run the following command to build the application:
mvn clean install
- Run the application: Use the following command to run the Spring Boot application:
mvn spring-boot:run
The application will be available at http://localhost:8080
.
You can interact with the API by sending HTTP POST requests to /execute
with your Java code as the body.
Example Request (using Postman):
- Open Postman and set the request type to
POST
. - Set the URL to
http://localhost:8080/execute
. - In the Body tab, select
raw
and set the type toJSON
. - Use the following Java code in the body:
{ "code": "public class Test { public static void main(String[] args) { System.out.println(\"Hello World\"); }}" }
{
"status": "Success",
"output": "Hello World",
"code": "public class Test { public static void main(String[] args) { System.out.println(\"Hello World\"); }}"
}
{
"status": "Failure",
"message": "Code contains syntax errors."
}
{
"status": "Failure",
"message": "Unsafe code detected."
}
The project comes with a comprehensive suite of unit and integration tests. To run the tests, execute the following command:
mvn test
- Execute Valid Java Code
- Execute Code with Invalid Syntax
- Check for Unsafe Code
- Memory Limit: 256 MB
- CPU Limit: 0.5 CPUs
- Network: Disabled for security
These limits ensure that user-submitted code cannot consume excessive resources or interact with external networks.
- The code execution is sandboxed in a Docker container to prevent potentially dangerous interactions with the host system.
- Only whitelisted Java code patterns are allowed to prevent malicious activities such as file I/O, network access, or infinite loops.