Open In App

ProcessBuilder in Java to create a basic online Judge

We have discussed Process and Runtime to create an external process. In this post, ProcessBuilder is discussed which serves the same purpose.
Let us understand an application where we need to get source code and find out the language. The application needs a string (containing source code) as input. The application needs to find out the language in which source code in written. Source codes will be having a relevant extension. For example – 
 

Using the name of input file the required language, to be used, can be found out.
Recall that compiling a code in java is done with command –
“javac Main.java”
and to execute it we use –
“java Main”
Similar commands are there for other languages. Therefore we need a separate text file containing all the commands which our software should execute one by one.
In case of error (Runtime or Compiler Errors) our application should write the error logs in a separate text file. Whatever output the source code is producing, our application should write it in another text file.
We use “ProcessBuilder” class in “Language” package which can execute OS processes. 
Now we must create a process first- 



ProcessBuilder pb = new ProcessBuilder("cmd"); 

Note that we are using “cmd” so that our commands can easily be executed in command prompt.
Suppose that we’re having our commands in “commands.txt” file and we wish to store output in “output.txt” along with error logs in “error.txt”. We should tell the ProcessBuilder object about them all. The “ProccessBuilder” class has methods – 
 

All grounds have been set, we just need to start our process. Invoking a process is just like a thread. We use- pb.start()
to start our process.
Below is a sample java code to compile and run another java code-
 






// Java program to demonstrate use of ProcessBuilder
// to compile and run external files.
import java.util.*;
import java.io.*;
class Main
{
    public static void main(String [] args) throws IOException
    {
        try {
            // create a process
            ProcessBuilder pb = new ProcessBuilder("cmd");
 
            // take all commands as input in a text file
            File commands = new File("E:\\test\\commands.txt");
 
            // File where error logs should be written
            File error = new File("E:\\test\\error.txt");
 
            // File where output should be written
            File output = new File("E:\\test\\output.txt");
 
            // redirect all the files
            pb.redirectInput(commands);
            pb.redirectOutput(output);
            pb.redirectError(error);
 
            // start the process
            pb.start();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

For the code to work correctly do the following steps- 
 

  1. Create a folder named Text in E directory.
  2. Create two empty text files named “error.txt” and “output.txt”.
  3. Create a text file named “commands.txt” which should have –
    javac Demo.java
    java Demo
  4. Create a file named “Demo.java” where the source code should be present and “main(String[] args)” function should be present in “Demo” class.
  5. Finally compile and execute the code written above.
  6. If there are any errors the logs would be present in “error.txt” file, last the output will be shown in “output.txt”.

Warnings: 
 

Advantages- 
 

Differences between Runtime.getRuntime.exec() and ProcessBuilder : 
Runtime.getRuntime.exec() executes the specified string command in a separate process. The ProcessBuilder, on the other hand, only takes a List of strings, where each string in the array or list is assumed to be an individual argument. The arguments are then joined up into a string, then passed to the OS to execute. 
 

   // ProcessBuilder takes a list of arguments
   ProcessBuilder pb =
   new ProcessBuilder("myCommand", "myArg1", "myArg2");

   // Runtime.getRuntime.exec() takes a single string
   Runtime.getRuntime.exec("myCommand myArg1 myArg2")

 


Article Tags :