13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

100Chapter 4Where Am I? Executi<strong>on</strong> C<strong>on</strong>text4.1WHAT YOU WILL LEARNWe’ll show you how Java provides access to the command-line parameters andenvir<strong>on</strong>ment variables. We’ll also discuss the Java RunTime and Propertyclasses. Java’s use of the standard input/output/error streams is also brieflycovered, al<strong>on</strong>g with an introducti<strong>on</strong> to those c<strong>on</strong>cepts. We’ll end with a shortword <strong>on</strong> portability c<strong>on</strong>cerns.4.2A SIMPLE STARTThe most basic external informati<strong>on</strong> that a program may use is the informati<strong>on</strong>supplied <strong>on</strong> its invocati<strong>on</strong>—simple parameters or arguments, such as filenamesor opti<strong>on</strong>s, that can direct its running and make it a more flexible tool. Let’sstart with getting at that informati<strong>on</strong> from a Java program.4.2.1 Command-Line ArgumentsWhen a program is run from the command line, more than just the programname can be supplied. Here are some examples:$ javac Hi.java$ mv Acct.java core/Account.java$ ls -lIn the first example, we invoked a program called javac and gave it theparameter Hi.java, the name of the file c<strong>on</strong>taining the Java program that wewant javac to compile to Java byte code. (We’ve got a whole chapter <strong>on</strong> howto set up and run the Java compiler, see Chapter 5.) The mv got two commandlinearguments, Acct.java and core/Account.java, which look a lot likepathnames. The ls command has <strong>on</strong>e argument, -l, which in <strong>Linux</strong> usuallyindicates, by its leading minus sign, that it is an opti<strong>on</strong> for altering the behaviorof the command. (In this case it produces the “l<strong>on</strong>g” versi<strong>on</strong> of the directorylisting.)Even point-and-click GUIs allow such parameters to be supplied, thoughoften not visible to the user. In KDE, <strong>on</strong>e can create a new desktop ic<strong>on</strong> thatis a link to an applicati<strong>on</strong>. Such an ic<strong>on</strong> has a property sheet that lists, <strong>on</strong> theExecute tab, the command to be run, including any parameters.In Java, the parameters supplied <strong>on</strong> the command line are available to themain() method of a Java class. The signature for this method is:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!