Run a batch file from Java

__Note: This article was confirmed in the following environment. __


Suppose you have helloworld.bat in a folder called C: \ sample. The contents are as follows.

helloworld.bat


@echo off
echo Hello World 01
echo Hello World 02
echo Hello World 03

Below is a sample of executing this helloworld.bat from Java.

//Generate ProcessBuilder
// helloworld.Connect stdin and stderr of bat to stdin and stderr of parent process.
var processBuilder = new ProcessBuilder("C:\\sample\\helloworld.bat");
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);

//Process generation
// helloworld.Execute bat as a child process and stop the parent thread until the process finishes executing.
var process = processBuilder.start();
process.waitFor();

When you do this, you should see a string similar to the following in Java's standard output:

Hello World 01
Hello World 02
Hello World 03

__ Personally, the "addicted" point is the extension of the batch file. The extension of the file you want to execute must be bat or cmd. __

In the world of so-called shell scripts, there is no specification for the extension of the file in which the script is written. I think that it is customary to use sh, but as long as you have execute permission, you can execute either txt or html, and there is no problem even if there is no extension.

On the other hand, in the Windows world, the extension is used to determine whether the file is a batch file. In other words, if the extension is bat or cmd, the file is regarded as an executable batch file.

What I was addicted to was "I tried to run a file with a non-extension bat or cmd with ProcessBuider or Process and it didn't work". I am a so-called open application developer. In that world (that is, Linux and Unix), if the execution authority is correctly granted, it can be executed regardless of the extension, so it is difficult to think that it is necessary to specify a specific extension. I didn't (´ ・ ω ・ `)

Recommended Posts

Run a batch file from Java
Run batch with docker-compose with Java batch
[Java] Create a temporary file
How to jump from Eclipse Java to a SQL file
Java11: Run Java code in a single file as is
Run Java application in Azure Batch
Upload a file using Java HttpURLConnection
Run node.js from android java (processing)
Run R from a tomcat-powered Java process on Amazon Linux
Access Teradata from a Java application
Try running a Kubernetes Job from Java
Why does Java call a file a class?
Deep Learning Java from scratch 6.3 Batch Normalization
If a person from Java learns PHP
How to run a GIF file from the Linux command line (Ubuntu)
java file creation
GetInstance () from a @Singleton class in Groovy from Java
Run Rust from Java with JNA (Java Native Access)
Read a string in a PDF file with Java
Run R from Java I want to run rJava
Connect to Aurora (MySQL) from a Java application
To become a VB.net programmer from a Java shop
Programming beginners learn PHP from a Java perspective-variables-
A story about trying to operate JAVA File
Java file generation from apk (Bytecode Viewer expression)
Create Scala Seq from Java, make Scala Seq a Java List
A bat file that uses Java in windows
[Java] Get a random value from an array
CICS-Run Java application-(1) Run a simple sample app
Ssh connect using SSHJ from a Java 6 app
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
[Java] File system operation
Call Java from JRuby
Submit a job to AWS Batch with Java (Eclipse)
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Studying java8 (such as reading a file using Stream)
[Java] Create a filter
[Spring] Read a message from a YAML file with MessageSource
java (split source file)
[Java] How to extract the file name from the path
Eval Java source from Java
Run PostgreSQL on Java
Call a method with a Kotlin callback block from Java
java core: chopped core file
[Java] Let's create a mod for Minecraft 1.16.1 [Basic file]
java build a triangle
Access API.AI from Java
From Java to Ruby !!
I tried OCR processing a PDF file with Java
Using the database (SQL Server 2014) from a Java program 2018/01/04
[Note] Create a java environment from scratch with docker
[Java] How to erase a specific character from a character string
I made a Wrapper that calls KNP from Java
How to convert a file to a byte array in Java
21 Load the script from a file and execute it
File path is not recognized when executing an external command from a Java application
Call a program written in Swift from Processing (Java)
[Java] How to convert a character string from String type to byte type
Activate Excel file A1 cell of each sheet in Java
How to store a string from ArrayList to String in Java (Personal)