Run Java Using Command Prompt / Terminal

Compile and Run Java Using Command Prompt

This tutorial will guide you how you can compile and run Jaa file using command prompt in windows operating system. Process is similar if you are using other operating system like Linux or Mac. 

Step 1 : Install Java JDK and JAVA_HOME

First of all make sure to install Java JDK and JAVA_HOME on your system. For that you can go through this video. 

You can check if you already have Java JDK installed on your system by opening command prompt and Just type java –version.

Step 2 : Create Java File

Now to create a java file just go to the folder you want like Desktop.

  •  Now just right click on mouse and click on New -> Text Document.
Create Text Document
  • Now just click on View -> Show -> File name extensions. This will show the extension of file. 
Show File Extensions
Show File Extensions
  • Now Just rename Sign the file and replace .txt extension with .java file extension. 
Rename the file extension into .java
Rename the file extension into .java

Step 3 : Write Java Program

  • Now, simply open the file with NotePad or any code editor. 
Open Java File With NotePad
  • Now just write Java Program.
class HelloWorld { 
public static void main(String[] args){
System.out.println(“Hello, World!”);
}
}
Write Java Program in Code Editor (Notepad)
Write Java Program in Code Editor (Notepad)

Step 4 : Run program using Command Prompt

Now to compile and run the java program using Command prompt . 

  • Open Start Start Menu and Search for Command Prompt and Open it.
Open Command Prompt
Open Command Prompt
  • Then Just type cd Desktop, to navigate into Desktop. 
Navigate into Desktop
Navigate into Desktop
  • Now just type javac HelloWorld.java, this command will compile the file. And show if there is any error. 
Compile Java Program
Compile Java Program
  • This command will create a .class file. 
  • Now just type java HelloWorld, this command will run the compiled class file. 
Run the compiled class file
Run the compiled class file

So, here you can see the output, “Hello, World!”. 

If you face any error following the above steps, watch this comprehensive video tutorial. 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top