Java is one of the most popular programming languages. It can run any program on JVM (Java Potencial Machine). The program will terminate as JVM stops. Usually, Java programs terminate when they reach the end of the program; however, there are some situations where you may need to terminate a program during the execution of specific conditions. In such scenarios, end that particular program with the help of methods supported by Java.

This blog will teach you how to end a program in Java. So, keep reading!

How to End a Java Program?

In Java, you can end a program using:

    • System.exit() method
    • return statement

Check out each of the mentioned methods one by one.

How to End a Java Program Using System.exit() Method?

You can end a Java program by using the “exit()” method of the Java “System” class. It terminates the currently running JVM.


To invoke the mentioned method, follow the given syntax:

Here, the System.exit() method has a parameter 0, which indicates that the program will terminate without any error.

Example 1: Printing Strings Before and After Ending a Java Program

In this example, we will terminate our program after printing one statement. In the main() method, we will first print a String “Java Programming” by using the System.out.println() method:

System.out.println(«Java Programming»);

Then, we will call the exit() method of the System class to terminate our program:

Lastly, we will try to print another String:

System.out.println(«Java Programming language»);


As you can see, the “Java Programming language” string is not displayed because the JVM is terminated before the execution of this line:

Example 2: Ending a Java Program Based on Condition

We will now terminate our program based on the condition added in an “if” statement, within the “for” loop. Firstly, we will create an integer array of even numbers:

int array1[]= {2,4,6,8,10,12,14,16,18,20};

We will print out the array values till the array element is greater than or equal to 10. If the added condition is evaluated as “true”, the program will print the specified message and terminate:

for (int i = 0; i < array1.length; i++) {
         if (array1[i] >= 10){
                System.out.println(«Program terminated due to exit() method»);

After executing the “for” loop, the message “Exit…” in the main() method will not print on the console because the program exits before the execution of this line:



The output shows the program was terminated when the value of the array was greater than or equal to 10:

Let’s head towards the other method!

How to End a Java Program Using return Statement?

The “return” statement is mostly used to return a value to a function; otherwise, the void return statement will end the execution of the current method or function.


Add the keyword “return” to the point where you want to program executed to be stopped:

Now, we can check the examples with return statements to terminate a Java program.

Example 1: Ending main() Method Using return Statement

The “return” statement can terminate a Java program if it is the last statement executed within the main() method. If you add code after a return statement, the Java program may crash and throw an “Unreachable code” Exception because after running the return statement, no other line will be executed:

public static void main(String[] args) {
    System.out.println(«Java Programming»);
    System.out.println(«Java Programming language»);


Example 2: Adding return Statement in “if” Condition

In this example, we will terminate the Java program with the “return” statement added in the “if” condition. It will act the same as the System.exit() method.

Firstly, we will create an integer array of even numbers named “array1”:

int array1[]= {2,4,6,8,10,12,14,16,18,20};

Next, we will print the values of array and terminate the program when an elements gets greater than or equals to 15:

for (int i = 0; i < array1.length; i++) {
    if (array1[i] >= 15){
            System.out.println(«Program Exit due to return statement»);

The statement added in the main() method will not be executed because the Java program will end before that:




We have provided all the necessary instructions for ending a Java program.


To end or terminate a program in Java, use the System.exit() method or a return statement. Both ways are helpful, but the most commonly used approach is the System.exit() method. The System.exit() method terminates the currently executing JVM, whereas the return statement is used to return values or stop the function execution. This blog discussed how to end or terminate a Java program.

Source link

Git has been a revolutionary technology and tool for every modern software developer. Version control tools let you keep track of the changes and preserve a source code snapshot if you need to revert the changes.

Whether you are working on a small or a massive software project, using a git repository to manage it can be more than beneficial.

Before using git on your system, you need to install and configure it. Git is cross-platform and can be installed on any system running Windows, macOS, or Linux.

On Windows, you may face the “git is not recognized as an internal, external command, operable program, or batch file” error even after installing git.

What causes “‘git’ is not recognized as an internal or external command, operable program or batch file.” Error?

There are three major causes of the error. These includes:

  1. Git is not installed on the target system.
  2. Git is not available in the PATH environment system
  3. The terminal session is not reloaded after installing git.

Fix #1 Git is not installed on the target system.

Before using git, you must ensure you have it installed and configured on your system. Although there are several methods of installing git on Windows, it is recommended to use the official git for Windows installer.

Open your browser and navigate to the resource below:


Select the installer version for your Windows system.

Merienda the download is complete, launch the installer wizard and follow along with the installation instructions.

You can accept the default values as they work well out of the box.

Under the “Adjusting your PATH environment.”, select “Git from the command line and 3rd-party software.”

The above option adds git to your PATH environment variable. This helps you access the git utility from any directory in your command prompt and any third-party software such as your code editor.

Continue with the setup wizard and complete the installation process. Merienda the installation is complete, open your CMD window and check the git version.

Fix #2 Git is not available in the PATH environment system

Suppose you have git installed and still getting the “‘git’ is not recognized as an internal or external command, operable program or batch file.” error. In that case, the error is caused by git missing from the PATH environment variable.

You can fix that by manually adding git in your system environment variable. You can run:

setx /M path «%path%;C:Program FilesGitcmd»

The command above should update your system’s environment variables to include the path to the git binary.

If you are looking for a graphical method, start by navigating to the git binary in file explorer.

By default, you will find it in either of these directories.

C:Program FilesGitcmd

C:Program Files (x86)Gitcmd

Select the full path in the Windows explorer path bar and click copy.

Press the Windows Key + R to launch the Run dialog.

Enter sysdm.cpl to launch the System properties window.

Under “Advanced,” select “Environment Variables.”

Go to “System Variables” and select “Path.” Then, click on Edit to edit the Path environment variable.

Click on New and paste the path you copied earlier.

Click on OK to save the changes.

Merienda completed, launch a new terminal window and run the where git command.

Fix #3 Reload Terminal Session

You will need to reload the session to update the environment variables if you have a terminal session open when installing git.

You can do this by closing and opening a new terminal session.


Using this tutorial, you discovered three significant causes of the “git command not recognized…” error. You also learned three ways to fix this error if it arises in your system.

Source link