Java: Program execution from Command Line and Eclipse with Arguments.


In the previous post First Java “Hello World” Program, you got some basic understanding for starting a Java Program. In this post, you will learn about to execute a Java Program from Eclipse and Commandline. Here consider both the case by passing with and without arguments to the main method().

Points to Remember

These are some points that need keep in mind while passing arguments from the eclipse or command line.

  • When you need to pass more than one argument. These values separated by spaces.
  • If passing an argument String value having space make sure to pass in the double quote (” “). For Example: “Saurabh Gupta”.
  • Always check for the length of  main() method argument otherwise you may get ArrayIndexOutOfBoundException if trying to access index beyond passed arguments.

Execute Java Program from Eclipse without Arguments

When your program not required any argument to pass in main() methods follow these steps to execute a program.

For Example :

public class TestProgram {

	public static void main(String[] args) {
		System.out.println("Hello World ! ");
	}
}

Output


Hellow World !
  • Step 1: Go to the Java Class file  “TestProgram”.java.
  • Step 2: Right-click on the file select option as  “Run As” -> “Java Application”.
  • Step 3: You will see the output in the console tab.

Execute Java Program from Eclipse with Arguments

When your program required arguments to pass in the main() method follow these steps to execute a program.

For Example:

In this program, main() method required three arguments to print for “First Name “, “Last Name ” and “Full Name”.

public class TestProgram {

	public static void main(String[] args) {
		System.out.println("Hello World ! ");

		System.out.println("Number of argument passed :"+args.length);
		//Check for number of argument passed from command line
		if (args != null && args.length == 3) {
			System.out.println("First Name :" + args[0]);
			System.out.println("Last Name :" + args[1]);
			System.out.println("Full Name :" + args[2]);
		}
	}
}

Output


Hello World ! 
Number of argument passed :3
First Name :Saurabh
Last Name :Gupta
Full Name :Saurabh Gupta
  • Step 1: Go to the Java Class file  “TestProgram”.java.
  • Step 2: Right-click on the file select option as  “Run As” -> “Run Configuration”.
  • Step 3: You will get pop up, Go to the “Arguments” tab pass arguments in the “Program Arguments” section as mentioned in the below screen for this example.
  • Step 4: Click on the Run Button.
  • Step 3: You will see the output in the console tab.

Eclipse Arguments Passing to java program

ss

ss

Java Program run from command line.jpg

 

 

ss

java program run commandline with argument

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s