Run an external process in Java

ProcessBuilder At the current site, there was a request to hit Shell from within Java, so as a result of investigating, it was introduced in this article ** ProcessBuilder * It seems that you should use something called *. I have diverted the linked code, but when I actually use it, I will dynamically change the call destination of the process ... So, I wrote a memo completely.

Call as many arguments as you like at the caller.
Ex) execute(new String[]{"ping","111,222,33,4"});
public class CallOutsideProcess {
	public void execute(String args[]) {
		try {
			Process process = new ProcessBuilder(args).start();
			InputStream is = process.getInputStream();

			/*Depending on the output of the character string etc. on the process execution side
If the character codes do not match, the characters will be garbled when receiving.*/
			InputStreamReader isr = new InputStreamReader(is, "Shift-JIS");
//			InputStreamReader isr = new InputStreamReader(is, "UTF-8");

			BufferedReader reader = new BufferedReader(isr);
			StringBuilder builder = new StringBuilder();
			int c;
			while ((c = != -1) {
				builder.append((char) c);
			//Storage of character strings output to the console
			text = builder.toString();
			//Exit code storage(0:Normal termination 1:Abnormal termination)
			int ret = process.waitFor();

		} catch (IOException | InterruptedException e) {

It was a content that I was likely to use in the future, so let's remember ... ~~ (... I wrote it so that I can remember it even if I forget it) ~~

Recommended Posts

Run an external process in Java
I sent an email in Java
Run Java application in Azure Batch
thread safe process in java language
Try an If expression in Java
Run an application made with Java8 with Java6
Process every arbitrary number in Java List
Java buffering process
Changes in Java 11
Rock-paper-scissors in Java
Java check process
When calling println etc. from an external Java class file in Processing
I want to send an email in Java.
Second decoction: Try an If expression in Java
How to solve an Expression Problem in Java
Get along with Java containers in Cloud Run
[java] sort in list
Read JSON in Java
Make Blackjack in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
I wrote about Java downcast in an easy-to-understand manner
[java] throw an exception
Comments in Java source
Azure functions in java
Run PostgreSQL on Java
Format XML in Java
Simple htmlspecialchars in Java
Hello World in Java
Add an external jar file in your IntelliJ project.
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Quickly implement a singleton with an enum in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
I tried using an extended for statement in Java
Date manipulation in Java 8
What's new in Java 8
How to write an external reference key in FactoryBot
Send a command to Spigot from an external process
Java11: Run Java code in a single file as is
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Use Java Web Start in an OpenJDK environment on Windows
Identify threads in the Java process that are wasting CPU
Run R from a tomcat-powered Java process on Amazon Linux
Display weather forecast using OpenWeatherMap, an external API in Ruby
Get attributes and values from an XML file in Java
Forcibly stop Java process by specifying PID in Windows PowerShell
Generate AWS Signature V4 in Java and request an API
Generate Stream from an array of primitive types in Java