Run Java Program from Command line





Hey guys in this post, we will about how to write Java program in Notepad and run it from command line.

Install Java JDK on your System


First we need to install Java JDK on your system, at the time writing this blogpost, latest version of Java JDK is 16. You can download the Jdk from the following link

https://www.oracle.com/java/technologies/javase-downloads.html

Add Java to System Environment Variables


Next we need to add java to the environment variables. Open your system environment variables, Create variable under System variables

03

Next edit the Path variable and give reference to the JAVA_HOME path

04

Next check the java version from command prompt

05

If you see the java version then you are good to go

Create Java Program in Notepad


Open notepad and add the following java code

class HelloWorld {
	public static void main(String[] args) {
		System.out.println("HelloWorld");
	}
}

Save the file in any of the directory. Make sure to give file name as class name. For ex: If you are class name is HelloWorld then give name as HelloWorld.java

Compile the Program


Open the command prompt and go the directory where you have saved the java file. execute the following command to compile the java file.

C:\workspace\java-workspace>javac HelloWorld.java

this will convert java code into byte code and created a new file HelloWorld.class. If there are any errors then it will show the errors in the console.

Run the Program


To run the java program enter the following command from the root of the directory, make sure that the .class file should also be present in the same directory

C:\workspace\java-workspace>java HelloWorld

You can see the output inside the console.

06

That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.




Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply