Articles

Compile and Run java program in command prompt

Compile and Run java program in command prompt


In this java tutorial, we will learn about:

  • compile java program in command prompt,
  • run java program in 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 java program.

 

But it is still a good idea to be aware about the process and commands required to compile and run java program in 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.

 

You can also go through the video tutorial on compile and run java program in command prompt.

 

 

Assuming name of java program file as ProgramName.java,

Command to compile java program in command prompt: javac

Command to run java program in command prompt: java

We will lean how to use these commands below to compile and run java program:

 

Lets create a source file named MyFirstProgram.java and then we will learn compile and run java program in command prompt.

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");

	}

}

Lets compile java program in command prompt.

 

compile java program in command prompt

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

 

Compile your java program

javac MyFirstProgram.java

When you compile java program in command prompt 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.

Path not set then follow below way to compile java program in command prompt

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,

compile and run java program in command prompt

 

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

 

Now since our program compiled sucessfully, lets run java program in command prompt.

 

run java program in command prompt

We can run java program in command prompt as below using java command:

java MyFirstProgram

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

Hello World

Hello Java

Hello Learning

 

This wraps up our session on compile and run java program in command prompt.

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
Sonu Pandit
Technology geek, loves to write and share knowledge with the world. Having 10+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :    Published Date : Jul 04,2022  
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!