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 –
- Code in C language will have “.c” extension
- Code in C++ will have “.cpp” extension
- Code in Java will have “.java” extension
- Code in Python will have “.py” extension
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 –
and to execute it we use –
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 –
- public ProcessBuilder redirectInput(File file)
- public ProcessBuilder redirectOutput(File file)
- public ProcessBuilder redirectError(File file)
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-
For the code to work correctly do the following steps-
- Create a folder named Text in E directory.
- Create two empty text files named “error.txt” and “output.txt”.
- Create a text file named “commands.txt” which should have –
- Create a file named “Demo.java” where the source code should be present and “main(String args)” function should be present in “Demo” class.
- Finally compile and execute the code written above.
- If there are any errors the logs would be present in “error.txt” file, lest the output will be shown in “output.txt”.
- Make sure that the code, you wish to compile and execute, doesn’t contain an infinite loop or something like that which may cause your program to run forever. You can take care of this case by allowing the source code to run for a fixed time. For Example – if code takes more than 10 seconds to run, throw an error saying something like “Time Limit Exceeded”. For this you may use “waitFor()” function in “Process” class.
- You must check that the code compiles correctly, only then you should try to run it, otherwise display the error log files.
- You can create your own online judge using this technique.
- It will help you to link your software with the OS directly.
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 argyments ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2"); // Runtime.getRuntime.exec() takes a single string Runtime.getRuntime.exec("myCommand myArg1 myArg2")
This article is contributed by Suyash Srivastava. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Java.lang.ProcessBuilder class in Java
- Basic Operators in Java
- Basic Type Base64 Encoding and Decoding in Java
- How to create Immutable class in Java?
- 10 Ways to Create a Stream in Java
- Different ways to create objects in Java
- Java | How to create your own Helper Class?
- Factory method to create Immutable Set in Java 9
- Factory method to create immutable Map in Java 9
- How to create a TreeMap in reverse order in Java
- JSwing | Create Translucent and Shaped Windows in Java
- Factory method to create Immutable List in Java SE 9
- JSwing | Create a Magnifying tool using Java Robot
- Java Swing | Create a simple text editor
- How to create a Java HashMap of user defined class type?