Results 1 to 4 of 4
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default What is the use of command-line arguments supplied to the main method

    I know that the String array that the main method receives as a parameter is an array of command-line arguments supplied by the to an application when it is launched.

    Specifically, I have read read about it here.

    More precisely, let's say you have:

    Java Code:
    public class Foo {
           public static void main(String[] args) {
              if (args.length < 2) {
                 System.out.println("Write your name and a site you like when you run this class");
              } else {
                 System.out.println("Yo " + args[0] + ", so you like " + args[1] + "?");
              }
           }
        }
    once you compile this and run it with:

    Java Code:
    java Foo Edmund Quora
    you get:

    Java Code:
    Yo Edmund, so you like Quora?
    The confusion is that when an application is packaged as a .jar file (which is an executable file) and is distributed, the user would simply double-click it to start/run it.

    But command-line arguments are passed to a program in a .java fileby a developer when it is compiled from the command-line, right?

    So how are the command-line arguments useful to the users (e.g. the public which will use the software app) of the application?

    Can you give me a simple example of a use-case in which a program is dependent on command-line arguments?

  2. #2
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: What is the use of command-line arguments supplied to the main method

    You are able to run an executable jar file from command line by this command : java -jar file.jar arg1 arg2
    So for example you could define two ways of running an app. Thus if someone or even you, want to test your executable jar you could run it via terminal in debugging mode (for example by sending the -DEBUF flag in main).. Or maybe if you want to define some program's properties at start up. So one way to do that would be from command line. I am sure that there are more practical reasons for running a program from command line but i can't think of any right now..

    "So how are the command-line arguments useful to the users (e.g. the public which will use the software app) of the application?"

    hmm let's say you create a video game that automatically starts in full graphics and i am not able to run it because my computer sucks.. One good way to run it in low graphics would be from command line.

    A lot of programs have such kind of properties for example take a look here : https://developer.mozilla.org/en-US/...d_Line_Options
    Last edited by ShadowWalker; 02-07-2016 at 08:42 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: What is the use of command-line arguments supplied to the main method

    simple example of a use-case in which a program is dependent on command-line arguments
    I have a program that plays a game. The program stores user profiles in the registry. There is a commandline arg that gives the userid.
    I've created shortcuts for each user with the userid as an arg on the commandline in the shortcut.
    When a shortcut is clicked, the program starts and loads the profile for the userid given on the commandline.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: What is the use of command-line arguments supplied to the main method

    I have a program that plays a game. The program stores user profiles in the registry. There is a commandline arg that gives the userid.
    I've created shortcuts for each user with the userid as an arg on the commandline in the shortcut.
    When a shortcut is clicked, the program starts and loads the profile for the userid given on the commandline.
    Your footnote says that we should ask a question if we don't understand your response. I have got the general idea but I am not fully clear about what you said.

    So basically there is a multi-player game, where each user has a userid and say their score and progress is persisted through the sessions, and this information is stored in their profiles in the Windows' registry.

    The game requires to be run from the commandline, and the user is required to run it with a commandline argument specifying the userid.

    I've created shortcuts for each user
    as in shortcuts (of the .exe files of our software apps) we create on our desktop in Windows, the icons we double click to start the application?

    ... with the userid as an arg on the commandline
    I think this is the only part I am clear about.. that the app should be run from the command line with the userid as an arg, so that the game starts for that particular user.

    userid as an arg on the commandline in the shortcut.
    What is meant by passing a commandline arg in the shortcut?

    Another question: Are there actually any real applications which require the users to run them from the command line?


    PS: Sorry for responding to your answer this late. I think I had thought to get back to it in the hope that I would be clear about it then or atleast clear about what I don't understand, and it skipped my mind.

Similar Threads

  1. Command-Line Arguments
    By smvolt in forum Eclipse
    Replies: 6
    Last Post: 12-21-2011, 02:48 PM
  2. command line arguments
    By evermore in forum New To Java
    Replies: 5
    Last Post: 03-28-2010, 06:08 AM
  3. command line arguments
    By jttslg in forum Advanced Java
    Replies: 23
    Last Post: 03-24-2010, 06:06 PM
  4. Command Line Arguments
    By Nakira in forum NetBeans
    Replies: 10
    Last Post: 02-04-2010, 04:45 PM
  5. Command line arguments help
    By may88 in forum New To Java
    Replies: 8
    Last Post: 12-08-2009, 02:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •