How to Compile and Run Java Program in CMD Using Notepad

How to Compile and Run Java Program in CMD Using Notepad

When you will reach at the end of this Tutorial, You are able to:

  • To download and Installation JDK
  • To Setting up Java Environment for Windows PC (JAVA_HOME)
  • Understand What and Why about JAVA_HOME Environment Variable.
  • Create .java file using notepad.
  • Write a Simple java program using notepad.
  • Compile and Run Java Program using cmd.

So, let’s learn Step by Step to Run and Java Program in CMD using notepad.

Step 1: 
Download JDK (Java SE Development Kit) form given Link:

Step 2: 
You will get jdk-16.0.1_windows-x64_bin.exe file. Now Install JDK

Step 3:  
After Installation, You have to Set Up Java path environment variable, for that
    - Go to system properties in windows 10
    - Go to Advanced System Settings, it will open the System Properties window.
    - Select Environment Variables from the properties window. It will open the Environment Variables window.
- In Environment Variable window, Under System Variables select path and click edit. This will Open Edit Environment variable window.

set java environment variable path

    - Now go to C:\  --> program Files -->Java ---> jdk-16.0.1 --> bin
    - In my case: C:\Program Files\Java\jdk-16.0.1\bin
    - Copy this path and go to Environment Variable dialogue.
    - In Edit Environment Variable  Window click new and paste the path(C:\Program Files\Java\jdk-16.0.1\bin) we have already copied.

java path,environment path ,cmd java,run java

- Click OK

Step 4: 
Setting Up JAVA_HOME PATH environment variable
    
    - Go to system properties in windows 10
    - Go to Advanced System Settings, it will open the System Properties window.
    - Select Environment Variables from the properties window. It will open Environment Variables.
    - Now go to C:\  --> program Files --> Java --> jdk-16.0.1
    - In my case: C:\Program Files\Java\jdk-16.0.1
    - Copy this path and go to Environment Variable dialogue.
    - In the Environment Variable window, Under System Variables click new. It will open the New System Variable window. 
java_home setup,path java in notepade

    - In New System Variable, write variable name : JAVA_HOME
    - In variable value name type: C:\Program Files\Java\jdk-16.0.1
    - Click OK, 

Before moving next steps, Let’s Understand the importance of the Environment variable.

What is JAVA_HOME?
    JAVA_HOME is the name of an environment variable on the operating system that points to the installation directory of JDK (Java Development Kit) 
    - Example: JAVA_HOME = C:\Program Files\Java\jdk-16.0.1
 
Why is JAVA_HOME needed?
    To create and compile Java applications/programs, you need to set up the PATH environment variable of the operating system. So development tools like Eclipse, NetBeans can be executed. If the PATH environment variable doesn’t contain a path to JDK, a Java-based program will give some error. 
    For example, typing java in the command prompt showing this error:
'java' is not recognized as an internal or external command, operable program, or batch file. Or Eclipse may prompt Error: Eclipse launch error no Java home found

Step 5: 

Now, We need to Check Java JDK is installed properly or not using CMD.

    - Go to run command using windows key + r

Steps to Run Java Program using Command Prompt

    - Type cmd

    - Now type java -version

    - It will display the version name 

    - Now You are ready to run your first java program.

How do I run a Java program in notepad?

Step 6: 

Open Notepad and create file with .java extension.

In our example, I have created hello.java file. ( The first letter of Filename Should be in capital form)

-          Open Notepad

-          Go to File à Save as

-          It will Open Save as Window

-          Now In Save as window type filename Hello.java and in save as type Select “All Files”.

-          Click On the Save Button.


Step 7: 

Now write and save given simple java program.

public class hello{

public static void main(String args[])

        {

                System.out.println("Welcome to Java learning with COmputer Bits");

                }

}

Note: The first letter of Class name and file name should be in capital form and both are equal.

Watch Tutorial Video on How to run Java program using cmd:


Step 8: 
Now we are Ready with hello.java file

Step 9: 
We have to open CMD with current working directory, where our hello.java file is.
   
To open CMD: 
press  Windows key + r to open RUN and then type cmd and press Ok.
- It will Open CMD.
- You need to get into current working directory using CD command.

 To open CMD with Current Working Directory:

o   Simply open the folder where our hello.java file is.

o   Then in the address bar type cmd.

o   It will Open cmd in the current working directory.

- Check out the below image for a better understanding:

java using notepad

-  Now type javac hello.java to compile it. It will generate byte code file with .class extension.

How to Run Java Program in CMD Using Notepad

-  In our example hello.class file will generate.

-  Now, To Run java program write java hello in cmd

-  If there is not any error then, we will get output.

-  That’s IT. Now you know how to set up, create and run java program.


Please share it with all your friends and loved ones! 
Unlock Learning ✌ | Keep Sharing | Stay Safe.😷

Post a Comment

0 Comments