ProcessBuilder in Java to create a basic online Judge


Online judge is a platform that serves to compile, execute and evaluate programming solutions to a given problem. It is widely used for problem solving and organizing programming contests. To create a basic online judge in Java using ProcessBuilder class, define an instance of ProcessBuilder and specify the name of program and commands as arguments

The ProcessBuilder class is used to create and manage operating system processes. It allows us to chain multiple processes, where the output of one process can be used as the input to another process. Also, it provides a variety of built-in methods such as redirectOutput(), redirectInput(), redirectError() and so forth to handle processes efficiently.

Steps to Create an Online Judge using ProcessBuilder

We need to follow a set of steps in a logical and organized manner as mentioned below −

Step 1

Create a Java source code file named ‘OnlineJudge.java’. Then, copy and paste the below code inside that file.

Example

public class OnlineJudge {
   public static void main(String[] args) {
      System.out.println("Welcome to Tutorialspoint!!")
   }
}

Output

OnlineJudge.java:3: error: ';' expected
      System.out.println("Welcome to Tutorialspoint!!")
                                                       ^
1 error

Step 2

Create two text files in your Local System. One is to store the errors encountered during the compilation and execution of code. The other one is to hold the Outputs after successful execution of code.

We have created and named the files as −

“error.txt”

“output.txt”

Step 3

Now, create another Java file named ‘Process.java’. Copy and paste the below code inside that file

Let’s discuss the algorithm of code.

Algorithm

  • Create an instance of the ‘ProcessBuilder’ class and pass the command used to compile a Java program file as an argument to its constructor. The command for compilation is “javac className.java”.

  • Pass the path of “error.txt” file as an argument to the constructor of ‘File’ class. We will redirect the errors to this file using ‘redirectError()’ method.

  • Start the first process using the built-in method ‘start()’.

  • Again, create another instance of ‘ProcessBuilder’ class and pass the command used to execute a Java program file as an argument to its constructor. The command for execution is “java className”.

  • Pass the path of “output.txt” file as an argument to the constructor of ‘File’ class. We will redirect the outputs to this file using ‘redirectOutput()’ method.

  • Begin the execution of second process using the in-built method ‘start()’.

Example

import java.io.*;
import java.util.*;
public class Process {
   public static void main(String[] args) throws IOException, InterruptedException {
      // To compile the code with the javac command
      ProcessBuilder pb1 = new ProcessBuilder("javac", "OnlineJudge.java");
      // To print the error into error file
      File error = new File("D:/Java Programs/error.txt");
      pb1.redirectError(error);
      pb1.start(); // starting the process
      // Execute the code using the java.classname command
      ProcessBuilder pb2 = new ProcessBuilder("java", "OnlineJudge");
      // To print the output into output file
      File output = new File("D:/Java Programs/output.txt");
      pb2.redirectOutput(output);
      pb2.start(); // starting the process
      System.out.println("Successfully Executed");
      System.out.println("Check your Output file");
   }
}

Compile the above java file using ‘javac Process.java’ and Run using ‘java Process’ on your system’s command line interface.

Output

Successfully Executed
Check your Output file

Output Snapshots

We can see the output of the ‘OnlineJudge.java’ inside output.txt file -

Once we deliberately miss the semicolon at the end of line 3 to check if the error log gets redirected to the error.txt file or not. You can see the error log here.

Conclusion

We started this article by introducing the online judge and its use case. Later, we understood the ProcessBuilder class along with its use. The next section discussed all the required steps to create an online judge platform using ProcessBuilder. This platform can execute not only Java programs but also other programming languages like C and C++. We just need to change the commands passed to the instance of ProcessBuilder.

Updated on: 21-Jul-2023

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements