Skip to the content
onlineexamguide
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Command line arguments in java in CMD & Eclipse Explained Tutorial

Command line arguments in java

Study and learn Interview MCQ Questions and Answers on Command line arguments in java . Attend job interviews easily with these Multiple Choice Questions.

what is command line arguments in java

Command line arguments in java

Command line arguments in java are nothing but the data passed to the Main method of a Java Class at the time of running the Java Program. This data will be in the form of Strings or a group of characters separated by spaces in general. Let us know more about passing command-line arguments to a Java program using CMD or Eclipse IDE tool.

Read this Java Tutorial on Compiling and Running Java programs using CMD and Eclipse.

Note: You can pass only String values and all Primitive literal values to the Main method. Parsing of data from String to the respective data type like int, float, double, long, char, byte etc is the work of the Java developer. You can parse an “int” value from the String using Integer.parseInt() method for example.

Command line arguments using CMD

Java Command Line arguments are of String Object type. The number of arguments can range from 0 to any. All command-line arguments are passed on to the MAIN method as a String Array only at Runtime. CMD is our DOS command prompt where we compile and run Java programs.

Let us check the below example that takes Student’s details entered as command-line arguments. We have converted String type argument to an int type manually.

public class CommandLineArguments
{
  public static void main(String[] args)
  {
    if(args.length >= 2)
    {
      String name = args[0];
      int age = Integer.parseInt(args[1]);
      System.out.println("Name= " + name + ", Age= " + age);			
    }
    else
    {
      System.out.println("No arguments passed.");
    }
  }

}

Now, let us pass arguments to the above Program using CMD or Command-Line like below.

C:\MyPrograms> java CommandLineArguments John 25

The output is as follow.

C:\MyPrograms> Name= John, Age= 25

Now, let us see the output of the program without passing any command line arguments.

C:\MyPrograms> java CommandLineArguments

The output is as follow.

C:\MyPrograms> No arguments passed.

It is a good practice to check the length of the String array “args” inside the Main method, before accessing it. Otherwise, the JVM throws an exception related to Arrays.

Java Command Line using Eclipse

Eclipse is a Tool to compile, run and manage Java Programs (Projects). It is also called an IDE (Integrated Development Environment). Unlike passing command-line arguments in DOS Prompt, the Eclipse tool provides a GUI screen to enter String arguments. There is no difference in the output whether you use DOS prompt or Eclipse to pass arguments.

Follow the steps to pass Command-Line arguments in Eclipse.

Step 1: Open the Eclipse Tool. Type the Java Program. Save it.

Step 2: Now Click on “Run” in the Menu and then choose “Run Configurations ..” option. You will see a Popup.

Eclipse Run Configurations option menu screen

Step 3: Under the tab “Arguments“, enter the arguments separated by spaces. Click the button Run at the bottom of the Popup.

Java Command Line arguments entering popup screen in Eclipse

Step 4: Check the Output of Java program under Console in Eclipse.

.

Command line arguments in java

[WpProQuiz 162]

Write a comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Atal Bihari Vajpayee | श्री अटल बिहारी वाजपेयी जीवन परिचय
  • सम्राट अशोक का जीवन परिचय (Emperor Ashoka)
  • Prithviraj Chauhan
  • बाल गंगाधर तिलक
  • स्वामी दयानंद सरस्वती
  • UPPSC Mines Inspector Recruitment 2022 Notification Out
  • AIIMS Delhi JR Vacancy 2022 [194 Post] Notification and Apply Online
  • भीमराव अम्बेडकर
  • डॉक्टर राजेंद्र प्रसाद का जीवन परिचय
  • श्रीनिवास रामानुजन का जीवन परिचय
  • Amnesty International day
  • World Economic Forum
  • UPSSSC VDO Syllabus and Exam Pattern 2022
  • RBI Officer Grade B Recruitment 2022
  • UKMSSB Assistant Professor Recruitment 2022 Apply Now 339 Post

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming Tests
  • C programming Tests
  • C++ programming Tests
  • Aptitude Tests

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2022
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2022
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2022 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.