Home » Core Java » Java Command Line Arguments Example

About Vaishnavie Jayendran

Vaishnavie Jayendran
I have done my Bachelor of Engineering in Computer Science. I have worked primarily in Java and related open source frameworks. Learning and exploring new technologies is my passion

Java Command Line Arguments Example

In this article, we will focus on understanding how to compile and run a Java program from the command prompt and an example for Java Command Line Arguments.

The prerequisite for our example is JDK has to be installed. I am using JDK version 1.8.0_211 and the Operating System is Windows.

Before we head straight to our example, let us begin by understanding system variables like Path, Classpath.

1. Setting PATH

In Java, the PATH is required to locate the JDK binaries like “java” or “javac” commands that are used to run and compile Java source code.

In order to set PATH, open command prompt and type –

C:\>set PATH=C:\Program Files\Java\jdk1.8.0_211\bin;%PATH%

Note: ;%PATH% in the above line is used to append the value to the existing one by not overwriting.

In order to verify if the PATH is correctly set, type –

C:\>echo %PATH%

2. Setting CLASSPATH

The classpath is a parameter in the JVM (Java Virtual Machine) which specifies the location of classes in directories or JAR files.

To set CLASSPATH, in the command prompt type –

C:\>set CLASSPATH=C:\dependencies.jar;%CLASSPATH%

In order to verify CLASSPATH, type the following in the command prompt –

C:\>echo %CLASSPATH%

3. Compiling a Java Program in command prompt

3.1 Example 1 – without package declaration

Use any text editor like Notepad or TextPad to type the code. Copy the below program and paste it there.

MyProgramInCmdLine.java
public class MyProgramInCmdLine{

	public static void main(String[] args){
		System.out.println("Compiled and run in cmd prompt");
	}
}

The above file has to be saved in the name of “MyProgramInCmdLine.java”. To compile the above program, change directory to the working directory (C:\JavaCommandLineEx in my case) where this file is saved and then type the following:

C:\JavaCommandLineEx>javac MyProgramInCmdLine.java

This will successfully compile and generate MyProgramInCmdLine.class if there are no compilation errors in the program.

3.2 Example 2 – with package declaration

Use any text editor like Notepad or TextPad to type the code. Copy the below program and paste it there.

MyProgramInCmdLinePkg.java
package com.javacodegeeks.basic;

public class MyProgramInCmdLinePkg{

	public static void main(String[] args){
		System.out.println("Compiled and run in cmd prompt - with package declaration");
	}
}

The above file has to be saved in the name of “MyProgramInCmdLinePkg.java” under the directories com\javacodegeeks\basic (In my case the entire directory structure is C:\JavaCommandLineEx\com\javacodegeeks\basic where C:\JavaCommandLineEx is my working directory). To compile the above program, change directory to the folder C:\ JavaCommandLineEx and then type the following –

C:\JavaCommandLineEx>javac com\javacodegeeks\basic\MyProgramInCmdLinePkg.java

This will successfully compile and generate MyProgramInCmdLinePkg.class under C:\JavaCommandLineEx\com\javacodegeeks\basic if there are no compilation errors in the program.

4. Running/Executing a Java Program

4.1 Example 1

Now, to run the above MyProgramInCmdLine.java, in the command prompt change directory to the working directory(C:\JavaCommandLineEx) and type java MyProgramInCmdLine.

C:\JavaCommandLineEx>java MyProgramInCmdLine
Error: Could not find or load main class MyProgramInCmdLine

If you encounter the above error, do the following:

C:\JavaCommandLineEx>set CLASSPATH=.;%CLASSPATH%

Now, when you re-run the program you should see this:

C:\JavaCommandLineEx>java MyProgramInCmdLine
Compiled and run in cmd prompt

4.2 Example 2

Now, to run the MyProgramInCmdLinePkg.java in the command prompt change directory to the working directory C:\JavaCommandLineEx and type java com.javacodegeeks.basic.MyProgramInCmdLinePkg

C:\JavaCommandLineEx>java com.javacodegeeks.basic.MyProgramInCmdLinePkg
Compiled and run in cmd prompt - with package declaration

5. Java command line arguments program

Command line argument is the information that is passed to the program at runtime. They are passed as a String array argument to the main() method. Inside the main method, the first argument is referenced by args[0], the second as args[1] and so on.

MyProgramInCmdLineWithArgs.java
public class MyProgramInCmdLineWithArgs{

	public static void main(String[] args){
		for (int i = 0; i < args.length; i++) {
            		System.out.println("args[" + i + "]: " + args[i]);
		}
	}
}

Execute the program like shown here –

C:\JavaCommandLineEx>java MyProgramInCmdLineWithArgs My example for cmd line with arguments 
Output:
args[0]: My
args[1]: example
args[2]: for
args[3]: cmd
args[4]: line
args[5]: with
args[6]: arguments

6. When to use command line argument?

Command line arguments are generally used to pass parameters to a standalone program which change periodically without a change in the functionality. Example – A report generation program that generates reports at various frequencies like daily, monthly, quarterly, yearly, etc. The frequency type can be passed as a command line argument while running the program.

7. Download the source code

This was an example of Java Command Line Argument.

Download
You can download the full source code of this example here: Java Command Line Arguments Example
(+1 rating, 1 votes)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of