Skip to content

How to Run Java Program in Command Line

Advertisement

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

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply

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


Back to top