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 ProgramName.java

Command to run Java Program: java ProgramName

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 


Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com
Page Views : 137    Published Date :Feb 26,2020   Author :rajneesh
Please Share this page

Popular Articles

Loading...
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!