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.

Advertisement

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

Share.
Leave A Reply


Exit mobile version