Javatpoint Logo
Javatpoint Logo

How to Run Java Program in CMD Using Notepad

In this section, we will learn how to save, compile, and run (execute) a Java program in Command Prompt (CMD) using notepad.

Before running (execute) a Java program, ensure that Java is installed in the system and the path is properly set. If the path is not properly set, we cannot run the Java program.

We must follow the steps given below to run a Java program.

  • Open the notepad and write a Java program into it.
  • Save the Java program by using the class name followed by .java extension.
  • Open the CMD, type the commands and run the Java program.

Let's create a Java program and run it using the Command Prompt.

Note: We are considering that Java is properly installed and the path is properly set in your system.

Step 1: Open the notepad by pressing the Windows Key + R, type notepad and press enter key, or click on the Ok button. It opens the notepad.

How to Run Java Program in CMD Using Notepad

Step 2: Write a Java program that you want to compile and run. We have written the following code in the notepad.

CharArrayToStringExample.java

Step 3: To save a Java program press Ctrl + S key and provide the file name. Remember that the file name must be the same as the class name followed by the .java extension.

If you are writing the same Java program (as above) save it by providing the file name CharArrayToStringExample.java press enter key or click on the Save button.

How to Run Java Program in CMD Using Notepad

We have saved the above program at the location C:\demo.

How to Run Java Program in CMD Using Notepad

Now, we have created and saved the Java program. In the next step, we will compile and run the Java program.

Step 4: To compile and run a Java program, open the Command Prompt by pressing Windows Key + R, type cmd and press enter key or click on the Ok button. It opens the Command Prompt window.

How to Run Java Program in CMD Using Notepad

Step 5: In the Command Prompt window, write the following commands.

Now we are inside the demo folder where we have saved the Java program.

How to Run Java Program in CMD Using Notepad

Step 6: To compile the Java program type the following command:

javac CharArrayToStringExample.java

How to Run Java Program in CMD Using Notepad

When we compile a Java program without any error, it creates a .class file with the same name as the file name at the same location (where the program is saved).

How to Run Java Program in CMD Using Notepad

Step 7: To run the Java program, type the following command.

java CharArrayToStringExample

Note that we have not written .java after the file name.

How to Run Java Program in CMD Using Notepad

In the above image, welcome to Javatpoint is the output of the program.


Next TopicJava Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA