Java How to – Compile and run Java program in Command-line

Published on February 18, 2016 by abundantcode

Filed under Java

Last modified February 17, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 2808 times

Problem Statement

You need to compile and run your java program in command-line.


You can use the command-line tools available with Java Development Kit (JDK).

The "javac" command tool can be used to compile the java program.

The "java" command can be used to run the java program.

Let’s build out first program and use the command line to run.

1. Download and install Java Development Kit (JDK) from

2. Open Notepad and copy the below code snippet in to it and save it as For demo we will be saving it in the path D:\javaprograms

public class test {

public static void main(String[] args) {

     System.out.println("Hello Abundantcode Java tutorial");



3. Open Command prompt from your Windows PC and set the current path to D:\javaprograms. Also, set the path variable to the path where the Java Development Kit is installed (Eg: set path=%path%;C:\Program Files\Java\jdk1.8.0_73\bin ) as shown below

D:\javaprograms>set path=%path%;C:\Program Files\Java\jdk1.8.0_73\bin


D:\javaprograms>java test

Hello Abundantcode Java tutorial



You should see the message displayed via println is displayed in the console window.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


You might be Interested in these Posts

C Program to display Inverted half-pyramid using *

Problem Write a program in C to print Inverted half-pyramid using * as shown below.The program shou...

How to use array Rank in C# ?

In C# , the Rank property of the array is is used to get the rank of the array. In simple terms , ra...

How to append an array to another in JavaScript ?

In JavaScript , you may want to append one array to another array(s) to combine their elements resul...