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 3870 times

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.

Leave a Comment


x

You might be Interested in these Posts

Video - Angular Router - Brian Ford

Video - Angular Router - Brian Ford http://www.youtube.com/watch?v=sfV0ApyN5fM ...

Video - An Angular2 Todo App: First look at App Development in Angular2

In this screencast , David East , one of the core contributor of the Angular provides a glimpse of A...

Video - Getting started with Angular 2

In this screencast , Alex Eagle , developer on the Angular team talks about Angular 2 developer prev...