Articles

Compile and Run Java program using command prompt

Compile and Run Java program using command prompt

If you are using java development tools like Eclipse, Netbeans, etc. for development purpose then you can simply use the menu provided to compile and run your java programs.

 

But it is still a good idea to be aware about the process and commands required to compile & execute java programs using the command prompt.

To be able to accomplish our task we will write a sample java code and then compile and execute it on command prompt and observe the output.

 

Assuming name of java program file as ProgramName.java,

Command to Compile Java Program: javac

Command to run Java Program: java

We will lean how to use these commands below:

 

Source File: MyFirstProgram.java

package basic;

public class MyFirstProgram {

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

	}

}

Open command prompt (Start → type cmd→ Click on Command Prompt).

 

Compile your java program

javac MyFirstProgram.java

When you compile your java source code using above command (assuming you have properly set your path and classpath) then a .class file with name MyFirstProgram.class will be created at the java classpath.

 

If path is not set then you need to be on bin folder of your JDK while using javac, use below command,

JDKPath\bin> javac MyFirstProgram.java

 

It looks somewhat like this on cmd,

javac command to compile java program

 

Above javac command is used to compile a java class named HelloJava.java which is assumed to be saved in d:\

 

 

Run your java program

java MyFirstProgram

 

When you run the above java program using above command on cmd, you get following output:

Hello World

Hello Java

Hello Learning

 

To Learn java in details click here 


Java Interview Questions

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Rohanjit Kumar
Technology geek, loves to write and share knowledge with the world. Having 9+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :    Published Date : Jun 28,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!