This week, we're going to discuss some details about the Java runtime environment, and a few of the objects that allow us to interact with it.Those of you who've spent time programming in MS/DOS or UNIX are probably familiar with the concept of environment variables; for those of you who aren't, an environment variable system is a "global" information management system that usually works at the operating system level. It allows user-written programs to store and retrieve data (usually strings) by binding it to names (the variable names) and maintaining it in memory.
In our case, the actual environment variables of your operating system are completely shielded from your Java applets, which is deliberate and important. One of the (supposedly) fundamental concerns of this language is security, and allowing any applet access to this part of your computer is a potentially very bad idea.
The metaphor of the environment variable remains, however. In fact, there is an entirely separate "environment" in which your applets will execute. Both the parameters that your applet receives when invoked and the "properties" provided by the runtime interpreter work in this manner. Methods provided for accessing and manipulating them are very similar to their counterparts, which exist in other languages that happen to have direct access to their host operating system.
Parameters, in point of fact, are actually more related to another peculiarity of certain operating systems, namely, the command-line option. Users of command-line-based operating systems such as MS/DOS and UNIX know command-line options as the additional text they add after typing a program name that's passed to the program itself on execution. Programmers in these environments know command-line options as sets of strings parsed out by the operating system and available for inevitable use as a guide to the user's wishes. Where Java is concerned, parameters are passed instead through HTML, in the following format:
<APPLET CODE="some-applet.class" WIDTH=100 HEIGHT=100> <PARAM NAME=COLOR VALUE=BLUE> <PARAM NAME=VOLUME VALUE=5> <PARAM NAME=TITLE VALUE="An example of a parameter."> </APPLET>
In this manner, many applets can be written so that not only do they cater to the whim of the "end-user" who views the Web page they reside upon (by way of their graphical interface), but also so that the person crafting the HTML page can pass them information or instructions--an immensely useful trait under many circumstances.As a programmer, it's simple to read what your users may or may not be telling your applet through parameters in their HTML by using the
getParameter()method. Simply, the method takes a String as an argument, corresponding to the NAME of the parameter, and returns a String, corresponding to the VALUE of the parameter, if there is one.We also mentioned Properties earlier; you may consider Properties to be Parameters like the above, with the exception that they are set by the Java interpreter rather than the HTML designer, or vice versa. Properties, rather than communicating how your applet might adapt to a particular usage in a particular bit of HTML, contain information about the environment that your applet is running in, regardless of specific usage. Properties are also stored as names with corresponding values.
To illustrate, according to the Sun Java Tutorial, applets have the ability to read the following system properties:
Key Meaning ------------------- ------------------------------ "file.separator" File separator (e.g., "/") "java.class.version" Java class version number "java.vendor" Java vendor-specific string "java.vendor.url" Java vendor URL "java.version" Java version number "line.separator" Line separator "os.arch" Operating system architecture "os.name" Operating system name "path.separator" Path separator (e.g., ":")
(In this case, the "Key" is the variable name.)As a programmer, accessing the information provided therein is done with the
System.getProperty()call. Once again (in its simplest form), it takes a String name as its argument and returns a String value if one exists.As a Java author, these seemingly innocuous features of the language can be critically important. Frequently, your applets will depend on information they retrieve from their environment variables (of sorts) to function properly and effectively. When we return next week, we'll delve still further into them.