Wednesday, February 24, 2010

How To Use Command Line Arguments in Eclipse

Colorado State University

How To Use Command Line Arguments in Eclipse



LINKS INTO DOCUMENT BELOW
Introduction Detailed Instructions using Welcome.java
Instructions using Args.java Instructions using TempConversion.java


http://www.cs.colostate.edu/helpdocs/eclipseCommLineArgs.html

How To Use Command Line Arguments in Eclipse

The main example used in these notes is the Welcome class,
developed in the WelcomeToJava Eclipse project.

This program accepts one input argument value,
which is automatically assigned to the first value
in String[] args, called args[0].
(Computer people tend to count from 0 and not 1.)

Study the program for a few minutes to find where args[0] is used.
Notice that it is of type String.

The detailed instructions are in the next section,
complete with screenshots from the Welcome class in Eclipse.

Two other example programs are described in the last section.


Detailed Instructions for Command Line Arguments in Eclipse
Example: Welcome.java

  1. Basically, it's simple.
    Click on Run -> Run (not Run Last Launched).

    In the window shown below,
    notice that the Welcome Java application is highlighted.

    In the large subwindow of the Run window, there is a set of tabs,
    labelled Main, Arguments, JRE, etc.
    Notice that the Main tab is highlighted.

  2. Click on the Arguments tab.
    This action brings up a window
    with a few places to enter values.
    You want to type in all your input values
    into the Program arguments window,
    with spaces (blank characters) between each value.

    For the Welcome program,
    only one input argument value is needed:
    the first name of a new Java student.
    For this run, we have chosen Sarah as
    the name of this student.

    Notice where it has been typed in.
    This is where all command line arguments should be entered.
    These values remain the same for all runs,
    until they are changed in this same window.

  3. Then just click Apply, followed by Run.
    The screenshot below shows the output in the console window.


Another Example of a Java program using Command Line Arguments: Args.java

Another example is the Args class,
developed in the TestArgs Eclipse project.
This program requires more than one command line argument.

  1. Click on Run -> Run (not Run Last Launched).

  2. Click on the Arguments tab,
    in the large subwindow of the Run window.
    This action brings up the Program arguments window,
    where you should type in your input values.

    For the Args program,
    eight input argument values are needed.
    For this run, we have chosen eight random sets of
    characters, some numeric, some alphabetic, as input values:
    namely, 101, Joe, Larry, 93214, 3.145, 66, ABCDEF, xxxxx.
    Notice that there is at least one space between every two arguments.

  3. Then just click Apply, followed by Run.
    The screenshot below shows the output in the console window.

  4. If you were to click on Run -> Run Last Launched at this point,
    the same command line arguments would be used automatically,
    producing the same output.

  5. To change the value of the command line argument,
    you need to click on Run -> Run again,
    and change the argument(s) in the Program assignment window.


Further Example of a Java programs using Command Line Arguments: TempConversion.java

Yet another example is the TempConversionCL class,
developed in the TempConversion Eclipse project.
This program requires only one command line argument,
but this value must be converted from a String
to a double value to be used arithmetically.

  1. Again, click on Run -> Run (not Run Last Launched).

  2. Click on the Arguments tab,
    in the large subwindow of the Run window.
    This action brings up the Program arguments window,
    where you should type in your input values.

    For the TempConversionCL program,
    only one input argument value is needed:
    the value of the Celsius temperature.
    For this run, we have chosen 28.5 for
    this value of the Celsius temperature.

  3. Then just click Apply, followed by Run.
    The screenshot below shows the output in the console window.



Copyright © 2003: Colorado State University for Computer Science. All rights reserved.

No comments:

Post a Comment