Java Reference
In-Depth Information
4.1
W HAT Y OU W ILL L EARN
We'll show you how Java provides access to the command-line parameters and
environment variables. We'll also discuss the Java RunTime and Property
classes. Java's use of the standard input/output/error streams is also briefly
covered, along with an introduction to those concepts. We'll end with a short
word on portability concerns.
4.2
A S IMPLE S TART
The most basic external information that a program may use is the information
supplied on its invocation—simple parameters or arguments, such as filenames
or options, that can direct its running and make it a more flexible tool. Let's
start with getting at that information from a Java program.
4.2.1
When a program is run from the command line, more than just the program
name can be supplied. Here are some examples:
Command-Line Arguments
$ javac Hi.java
$ mv Acct.java core/Account.java
$ ls -l
In the first example, we invoked a program called javac and gave it the
parameter Hi.java , the name of the file containing the Java program that we
want javac to compile to Java byte code. (We've got a whole chapter on how
to set up and run the Java compiler, see Chapter 5.) The mv got two command-
line arguments, Acct.java and core/Account.java , which look a lot like
pathnames. The ls command has one argument, -l , which in Linux usually
indicates, by its leading minus sign, that it is an option for altering the behavior
of the command. (In this case it produces the “long” version of the directory
listing.)
Even point-and-click GUIs allow such parameters to be supplied, though
often not visible to the user. In KDE, one can create a new desktop icon that
is a link to an application. Such an icon has a property sheet that lists, on the
Execute tab, the command to be run, including any parameters.
In Java, the parameters supplied on the command line are available to the
main() method of a Java class. The signature for this method is:
Search WWH ::




Custom Search