< Zurück | Inhalt | Weiter >

Chapter 4


Where Am I? Execution Context


image

Java claims—and rightly so—to be a “compile once, run anywhere” language. But when a program starts, that “anywhere” is now a specific somewhere. When running a Java application on Linux, or any environment for that matter, the question arises, “Where am I?” (context, environment, familiar landmarks). What can a Java program find out about its environment? In particular, on the Linux platform, (how) can we get at:

• Command-line parameters?

• The current shell’s environment variables?

• The current working directory?

• The location of data files?

The answers to these questions will depend on what kind of Java applica- tion you are creating, and just how portable you want your application to be.

image


99


4.1 WHAT YOU WILL LEARN


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 SIMPLE START


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 Command-Line Arguments

4.2.2 Unit Testing Made Easy

4.3.1 Java and Standard I/O

4.3.2 Environment Variables

4.3.3 Java and Environment Variables

4.5.2 Portability