How to Use Notepad++ to Compile and Run Java Programs

Sheeraz Gul Feb 02, 2024
How to Use Notepad++ to Compile and Run Java Programs

Notepad++ is not just your ordinary text editor; it’s a highly versatile and extensible platform that can cater to a wide range of programming languages thanks to its plugin system.

In this tutorial, we’ll focus on one specific use case: how to use Notepad++ to compile and run Java programs. While Notepad++ doesn’t natively execute Java code, it can be transformed into a powerful Java development environment with the right plugins and configurations.

Use Notepad++ to Compile and Run Java Programs

If you want to run Java programs within Notepad++, you can easily achieve this by following these steps:

  • Prerequisites

    Before you begin, ensure that you have Java Development Kit (JDK) and Notepad++ installed on your system. If you haven’t already installed them, you can download and install them from the following links:

    Once you have these prerequisites in place, proceed to the next step.

  • Install the NppExec Plugin in Notepad++

    Open Notepad++ and navigate to the Plugins menu. Then, choose Plugins Admin to open the plugin manager.

    Plugin Menu

    Look for the NppExec plugin in the list and install it if it’s not already installed.

    Install NppExec

    Note: If the NppExec plugin is already installed, it won’t appear in the list.

  • Set the Environment Variables

    To configure environment variables for Java, follow the instructions below:

    Open the start menu and search for Environment Variable.

    Search Environment Variable

    Click on Edit the system environment variables.

    Open Environment Variable

    In the user variable section, click New and add a variable named JAVA_HOME. Set the variable value to the directory where your JDK is installed.

    JAVA_HOME

    Next, find the Path variable in the system variables list and click Edit.

    System Path

    Click New and add %JAVA_HOME%\bin to the list of paths. Click OK three times to save and close the properties menu.

    System Path Variable

    At this point, you will need to restart your computer for the environment variables to take effect.

  • Run Java Program in Notepad++

    Now, you’re ready to compile and run Java programs in Notepad++!

    Open Notepad++, create a new file, write your Java program, and save it with a .java extension (e.g., Java_notepadpp.java).

    Save .java

    Go to the Plugins menu and select Execute NppExec Script from the NppExec submenu.

    Save Commands

    In the script box that appears, enter the following commands:

    cd $(CURRENT_DIRECTORY)
    javac $(FILE_NAME)
    java $(NAME_PART)
    

    Save these commands as Java_Run.

    Save Commands As Java_Run

    With the commands saved, you can now run Java programs.

    Again, go to Plugins and select Execute NppExec Script from NppExec.

    Execute NppExec Script

    Choose Java_Run and click OK to run your Java file in the console. You will see the program’s output in the console.

    Run the Java File in the Console

    There you have it! You have successfully compiled and run a Java program within Notepad++. Feel free to explore more Java programming in Notepad++.

    As an example, here’s a simple Java program:

    public class Java_notepadpp {
      public static void main(String[] args) {
        System.out.println("This is a demo to run a Java program in Notepad++");
      }
    }
    

    Let’s run it to see the output:

    Final Output

    Your Java program is now successfully compiled in Notepad++.

Now, whenever you want to compile and run your Java program in the future, go to the Plugins menu again.

Select Execute NppExec Script from the NppExec submenu.

Lastly, choose the script you saved in the previous step (e.g., Java_Run) and click OK.

This will compile and run your Java program, and any compilation errors will be displayed in the console.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook