How to Run the Java Code in Git Bash?
Last Updated :
28 Apr, 2025
Git Bash is a command-line interface that provides a Unix-like environment on Windows. You can use it to compile and run Java programs just as you would in traditional terminals on Linux or macOS.
Step to Run Java Code in Git Bash:
1. Install Java
Make sure you have Java installed on your Windows computer. You can download and install Java from the official Oracle website or use OpenJDK which is an open-source alternative.
Verify the installation by running the following command in your Git Bash terminal to see if it displays the installed Java version.
$ java -version
2. Write Your Java Code
Create a Java source code file using the text editor or an Integrated Development Environment (IDE) like Visual Studio Code, Eclipse, or IntelliJ IDEA.
- Save the file with a .java extension.
- For example, you can create a file named MyProgram.java.
Java
// Testing Code
public class MyProgram {
public static void main (String[] args) {
System.out.println("Hello, World!");
}
}
3. Open Git Bash
Launch Git Bash on your Windows machine. You can find it in Start menu if you have it installed.
4. Navigate to the Directory with Your Java Code
Use the cd command to navigate to directory where your Java source code file is located.
For example, if your code is in the C:\Users\YourUsername\Projects directory.
cd /c/Users/terminals/Projects
Replace YourUsername and Projects with your actual directory path.
5. Compile Your Java Code
In Git Bash, we can compile your Java code using javac command as follows:
javac MyProgram.java
If after successful compilation a .class file with same name as your Java class.
6. Run Your Java Program
After successfully compiling your code you can run it using java command followed by the name of the class containing the main method.
For example:
java MyProgram
If your code contains a main method this command will be execute your Java program and you should see the output in the Git Bash terminal.
7. Output for Your Java Code
A complete overview of command and their output is shown below:

Similar Reads
How To Get The Current Branch Name in Git? In Git, a branch represents an independent line of development. When working with Git, it's often necessary to know which branch you're currently on. This can help you avoid making changes in the wrong branch and ensure you're following your workflow correctly. This article will cover various method
2 min read
How to Create a New Branch in Git and Push the Code? Branching in Git is a helpful feature for software developers working on a big team project. It allows the team members to work on different aspects of the software by creating a branch from the main branch. The main branch is not affected by the changes in the created branch until it is merged into
8 min read
How to add remote origin in git? Git, most popular version control system, revolutionized the way developers collaborate and manage code. One important feature of Git is remote repositories, which serve as centralized hubs for code collaboration. In this article, we'll explore the process of adding a remote origin to your Git repos
2 min read
How to Install jq in Git Bash? jq is a lightweight and flexible command-line JSON processor. It allows you to slice, filter, map, and easily transform structured data. This guide will walk you through installing jq in Git Bash on Windows. Steps to Install jqStep 1: Download jqFirst, you need to download the jq executable for Wind
2 min read
How to Integrate Git Bash with Visual Studio Code? prerequisitesGit Bash is an application that provides Git command line experience on the Operating System. It is a command-line shell for enabling git with the command line in the system. VSCode is a Text editor that provides support for development operations and version control systems. It provide
2 min read
How To Navigate To The Git Root Directory? Navigating to the Git root directory is a common task for developers working with Git repositories. The git root directory is the top-level directory of a Git repository, containing the .git directory where Git stores all its configuration and history information.Why Navigate to the Git Root Directo
2 min read