Skip to content

How to Run Java Program in Command Line

How to Run Java Program in Command Line on Windows, Linux operating systems. Java hello world example program. Create and run first java program

Category
Programming
Covers
Java
Versions & support
About Java →

Run Java Program in Command Line. To run a Java program, you first need to compile it. After compiling the java program a .class file created with the class name. Now you can run this class file.To compile java programs you must have JDK installed on your system. If you don’t have already installed, visit one of following links to install and configure Java on your system.

Run Java Program in Command

Firstly, create a sample Java program in a .java file. Add below code to HelloWorld.java file and save it. The file name must be the same as class name with the main function. So if you want to change the file name, then change class name as well.


class HelloWorld {

	public static void main(String args[]){ 
	
		System.out.println("Hello World!");
	}
}

Now compile the HelloWorld.java file using java compiler. This will create a HelloWorld.class file in current directory.

$ javac HelloWorld.java

Finally, run your java program which ‘java’ command followed by your class name.

$ java HelloWorld

On the successful run, you will see output like below.

Run Java Program in Command

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

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


Back to top