Java Run System Command Example
How To Run System Commands From Java Applications
In one of my previous articles, we opened a Chrome browser in Remote Debugging Mode by manually typing and running commands in the terminal/command prompt. But there is a better way to achieve the same result by implementing a process to run system commands from within Java.
There are three parts to running external system commands in Java. They are preparing system command strings, building process and reading input and writing the output. Let's look at each part in order.
1. Preparing System Commands
As these are external commands, the ability to run them will depend on the operating system which you are running the Java application on. For example, although you can run the "ls" command in a Linux environment to get the list of files in the current directory it won't work in a Windows environment as "ls" is only available in *nix based operating system. You can use the "dir" command instead in a Windows environment.
Although this will limit the portability of the application provided by JVM, as a workaround you can have a separate string literal class to store all the required system commands for *nix and Windows and use them appropriately after checking the operating system that application is running at runtime. In the following example, system commands used to start the Chrome web browser in remote debugging mode in both environments are stored in a System Commands class.
2. Building Process
As mentioned before there are two approaches which can be taken when running external system commands from Java applications.
2.1 Using Java Runtime Class
The traditional way to run external system commands is by using Java Runtime class. In Java, Runtime class cannot be instantiated explicitly. But you can obtain a Runtime object using,
Runtime runtime = Runtime.getRuntime();
After obtaining a Runtime object, you can run a system command by either passing a complete system command with arguments as a one-string or an array of strings with the command and each argument as separate strings to the exec method. As soon as the "exec" method is called the input command will be run. exec() method returns a Process object which can be used for the next step. We can directly get the process without creating the Runtime object as follows.
2.2 Using Java ProcessBuilder Class
A new way to run external system commands was introduced in JDK 5.0 which was using Java ProcessBuilder class. Although the command and arguments can be given as a single string literal when using Runtime class, when using ProcessBuilder arguments must be provided as separate strings. ProcessBuilder class provides additional control over the nature of the process built to run our system command compared to Runtime class. There are a number of options you can customize to customize behavior of the process. You can refer to Oracle official documentation for more details.
You can now run the above system commands as follows.
Note that the system command string has been split using spaces and what passes into executeCommands() method is an array of system commands and arguments.
Now you have all the knowledge to launch Chrome Browser from within the Java Application. Next, let's look at how to read input stream and write to the output stream of the process we just created.
3. Handling Input and Output
Although handling input and output streams of a process is not a requirement in the case of launching Chrome browser there may be instances you may require to do so. For example you may want to get the list of files in a directory or write admin password to the output stream when running "sudo" commands in a Linux environment. Here I will explain only how to read the input stream as writing to output stream is a more complicated task.
You can get the input stream of the process which is essentially what you see if the command is run in terminal or command prompt with
process.getInputStream();
Errors are written in a separate stream in Java in default. You may write errors to a log file or redirect it to the input stream, so you don't have read two separate streams. To read messages from input stream you can use a Buffered Reader instance.
The following code presents the complete code to get and print the list of files in a directory in a Linux environment.
Conclusion
External system commands can be run in Java using either Runtime class or ProcessBuilder class. ProcessBuilder and Runtime classes have their differences but ProcessBuilder class provides more room for customization and control of the behavior of the process. It should be noted that the portability of your Java application can be reduced by running external system commands within Java as external system commands are operating system dependent. To avoid the above problem you may have to implement commands for each operating system separately.
Source: https://harith-sankalpa.medium.com/how-to-run-system-commands-from-java-applications-a914223edd24
0 Response to "Java Run System Command Example"
Post a Comment