Using Batch Files to Compile and Run Java Programs
If you have gone through our previous lessons on compiling and running programs from the command line, you should understand that it is useful to save these commands somewhere. This is where special batch (.bat) and shell (.sh) files come in. You can create files with the .bat or .sh extensions that contain multiple commands. Batch files are used for Windows, while shell scripts are used for Linux and macOS.
In this article, we will look at examples using batch (.bat) files. These files can be executed both from the command line and from Windows Explorer. Let's see how it works.
We have a project containing the src and classes folders. A run.bat file is also added to the root directory:

Let's take a look at its content:
javac -d classes -cp src src/first/Example1.java
java -cp classes first.Example1 As you can see, this batch file contains two commands. The first command compiles the Example1.java file, and the second command runs it.
Now let's run the batch file from the command line. Press Windows+R and type cmd:

Navigate to the required directory. To run the batch file, simply type its name:

This executes the two commands specified in the file.
The second way to run a batch file is to simply double-click on it. In this case, the batch file runs, executes all commands, and the window immediately closes. To prevent the command window from closing right away and to see the results, add the pause command at the end of the batch file:
javac -d classes -cp src src/first/Example1.java
java -cp classes first.Example1
pause Now the command window will stay open after execution, displaying the message: "Press any key to continue." Once you press a key, the window will close.
For Linux and macOS, an equivalent script is created in a file with the .sh extension. The content is almost the same, but the classpath path separator is a colon instead of a semicolon:
javac -d classes -cp src src/first/Example1.java
java -cp classes first.Example1 To run a shell script, you first need to make it executable with chmod +x run.sh, then run it as ./run.sh.
Frequently Asked Questions
What is a batch file and why is it useful?
A batch file (with the .bat extension) is a text file containing a set of commands for Windows. Instead of typing the compile and run commands manually every time, you save them in a batch file and execute them all at once with a single run.
What is the difference between a .bat file and a .sh file?
They serve the same purpose but for different systems: .bat is used on Windows, while .sh (a shell script) is used on Linux and macOS. The javac and java commands inside are the same; only the classpath path separator differs: a semicolon on Windows and a colon on Linux/macOS.
Why does the window close immediately after running a batch file?
When you double-click the file, the window closes as soon as the commands finish, so you can't see the result. To keep it open, add the pause command at the end of the file — the message "Press any key to continue" will then appear.
How do I run a batch file?
Two ways: double-click the file in Windows Explorer, or open the command prompt (Windows+R, then cmd), navigate to the directory, and type the file name, for example run.bat.
With batch files, you can compile and run your programs from the command line more efficiently.
Comments