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


This content was proof-read by Grammarly. You can Try Grammary for FREE

Problem Statement

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

Solution

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 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

2. Open Notepad and copy the below code snippet in to it and save it as test.java. 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>javac test.java

D:\javaprograms>java test

Hello Abundantcode Java tutorial

D:\javaprograms>

clip_image001

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

%d bloggers like this: