Richard G Baldwin (512) 223-4758, baldwin@austin.cc.tx.us, http://www2.austin.cc.tx.us/baldwin/

Setting Program Attributes

Java Programming, Lecture Notes # 52, Revised 10/06/99.

Preface
Introduction
Setting Up and Using Properties
Command-Line Arguments
Applet Parameters
Review

Preface

Students in Prof. Baldwin's Introductory Java Programming classes at ACC are responsible for knowing and understanding all of the material in this lesson (except that they are not responsible for detailed information that is specific to C++).

The detailed material on C++ is provided as supplementary material for the benefit of persons already familiar with C++ who are making the transition into Java.

Introduction

Sometimes a program needs to be able to read system attributes and read and/or modify program-specific attributes.

We will discuss system attributes in a subsequent lesson.

Java programs can manage program attributes through three mechanisms:

Setting Up and Using Properties

The topic of Properties is one of those topics which is not technically challenging, and which is also somewhat boring until, if, and when you need to know about it. But, when you need it, you really need it.

Therefore, this discussion will be brief and generally non-technical. When the day comes that you need to know how to use properties, go to The Java Tutorial, page 227, or some other good reference to obtain the details.

According to The Java Tutorial by Campione and Walrath,

"Properties define environmental attributes on a persistent basis. That is, use properties when attribute values need to persist between invocations of a program."

In other words, if there is information that needs to be saved on a long-term basis and provided to your program each time the program is invoked, then properties may be the answer. Of course, this implies that your program has access to long-term storage, which in turn implies access to disk files.

As you may recall, while Java applications have access to disk files on the host computer where the application is being executed, as of the date of this writing (1/26/97) applets are denied such access.

Therefore, in order for an applet to make use of properties, it would be necessary for the applet to have access to long-term storage on the server from which it was downloaded (also of this writing, applet access to network servers is limited to the server from which it is downloaded).

The Properties Class

Properties are represented by objects of the Properties class. These objects can be stored on disk and retrieved when needed by a program which has appropriate disk access.

Properties are stored in the instance variables of the object in key/value pairs. Each individual property is identified by a key value and the value of the individual property is stored in the value member of the pair.

Both the key and the value are strings. For example, os.name is the key for one of Java's default system properties--its value contains the name of the current operating system.

The following program illustrates the use of a Properties object to display the default system properties on my computer at a specific point in history. The output from the program is shown in the boldface comments at the beginning of the program.

/*File Prop01.java Copyright 1997, R.G.Baldwin
Illustrates use of an object of type Property to display 
system properties.

The output from the program was:
-- listing properties --
user.language=en
java.home=C:\JAVA_JDK\JAVA\BIN\..
awt.toolkit=sun.awt.windows.WToolkit
file.encoding.pkg=sun.io
java.version=1.1.3
file.separator=\
line.separator=

user.region=US
file.encoding=8859_1
java.vendor=Sun Microsystems Inc.
user.timezone=CST
user.name=Baldwin
os.arch=x86
os.name=Windows 95
java.vendor.url=http://www.sun.com/
user.dir=C:\BALDWIN\JavaProg\Combined\Java
java.class.path=.;c:\Baldwin\JavaProg;C:\JAVA_JDK\JAV...
java.class.version=45.3
os.version=4.0
path.separator=;
user.home=C:\JAVA_JDK\JAVA\BIN\..
**********************************************************/
import java.util.*;
import java.lang.*;

class Prop01{
  public static void main(String[] args){
    //Instantiate and display a Properties object 
    // containing the system properties
    Properties obj = new Properties(
                                 System.getProperties() );
    obj.list(System.out);
  }//end main()
}//end class Prop01

As you can see, this is essentially a two-line program. The program instantiates a Properties object and stores the default system properties in that object.

The system properties are obtained by calling the class method named getProperties() of the System class. The list() method of the Properties class is then used to display the contents of the object.

Your program will need to use the Properties class to maintain properties which are specific to your Java program.

As in the above program, System properties are maintained by the java.lang.System class.

Once you have created a Properties object for your program, you can save it to an output stream using the save() method. You can later retrieve it using the load() method.

Command-Line Arguments

Sample Program Showing Command-Line Arguments
Conventions
Differences Relative to C and C++

The second mechanism that Java programs can use to manage program attributes is through the use of command-line arguments.

Command-line arguments are used to define attributes for Java applications on a non-persistent basis. You can use command-line arguments to set one or more attributes for a single invocation of an application.

According to The Java Tutorial by Campione and Walrath:

"Note: The Java language supports command line arguments. However, for some systems (such as Macintosh) command line arguments don't make sense. To avoid platform dependencies in your Java applications avoid the use of command line arguments and use properties instead."

Command-Line Arguments are information entered by the user following the name of the program on the command line. DOS users will be familiar with operating system commands such as

copy fileA fileB

In this case, copy is the name of the program to be executed while fileA and fileB are arguments which provide necessary information to the program.

A Java application can accept any number of arguments from the command line.

These arguments make it possible for the user to affect the operation of an application.

When you invoke an application, the runtime system passes the command-line arguments to the application's main method via an array of String objects. Each String object referenced by an element in the array contains one of the command-line arguments.

Sample Program Showing Command-Line Arguments

The following program echoes the command-line arguments to the screen.

/*File cmdlin01.java Copyright 1997, R.G.Baldwin
This Java application illustrates the use of Java 
command-line arguments.

When this program is run from the command line as follows:

java cmdlin01 My command line arguments

the program produces the following output:

My
command
line
arguments

**********************************************************/
class cmdlin01 { //define the controlling class
  public static void main(String[] args){ //main method
    for(int i=0; i < args.length; i++)
    System.out.println( args[i] );
  }//end main
}//End cmdlin01 class.  

As mentioned above, the runtime system passes the arguments to the program as an array of references to String objects. All array objects have a length parameter which contains the number of elements in the array.

This program uses the length parameter of the String array in the conditional clause of a for loop to display each of the strings in succession.

One practical issue that you must face is that all arguments are passed in as strings. Often this is not what is required by the program. For example, the program may need numeric information or boolean information. This generates a requirement to convert the string arguments to numeric information, boolean information, etc.

A previous lesson contained a sample program showing how to use the numeric wrapper classes to convert strings to numeric values. You might want to go back and review that program.

Individual command-line arguments are separated by the space character. Notice that the above application displays each word on a line by itself. Each word in the program is an individual argument.

If you want a single argument to include terms separated by a space, enclose the terms that are to be treated as a group in double quotes.

Conventions

A whole set of conventions has arisen regarding the format and usage of command-line arguments. On one hand, this is not a technical issue. On the other hand, it gives rise to technical implementation issues because the conventions typically allow the user to enter the arguments in any order, often in any case, etc.

The Java Tutorial by Campione and Walrath describes a number of existing conventions and also provides a sample program which can deal with the parsing issues raised by those conventions. We won't discuss that material here. You should simply remember where to find the conventions and the sample program when you need it.

Differences Relative to C and C++

C and C++ programmers should note that the Java method of passing command-line arguments is similar to that used in C and C++, but different in several respects.

The syntax of the main function a C/C++ program is not required to support command-line arguments if they are not being used.

However, the syntax of the main function in a Java program must support command-line arguments even if they are not used.

The system passes two parameters to a C/C++ program in support of command-line arguments:

  • The number of arguments
  • A pointer to an array of pointers, each of which points to a string.

However, the system passes only one parameter to a Java program in support of command-line arguments: an array of references to objects of type String. The number of strings in the array can be determined using the length variable of the array object.

In C and C++, the system passes the name of the program being executed in addition to the actual arguments entered on the command line (in some systems, the name of the program is garbage).

However, in Java, only the actual arguments are passed to the program.

Applet Parameters

The third mechanism that Java programs can use to manage program attributes is through the use of applet parameters.

Applet parameters can be used within HTML pages to affect the behavior of an applet much as command-line arguments can be used to affect the behavior of an application. You can use applet parameters to set one or more attributes for a single invocation of an applet.

We will defer discussion of this applet parameters until the lessons which discuss applets in detail.

Review

Q - Write a program that meets the following specifications.

/*File SampProg27.java from lesson 52
Copyright 1997, R.G.Baldwin
Without viewing the solution that follows, write a Java
application that illustrates use of an object of type 
Properties to display system properties.

The output from the program should be similar to the
following, but will differ from one system to the next:
  
-- listing properties --
user.language=en
java.home=C:\JAVA_JDK\JAVA\BIN\..
awt.toolkit=sun.awt.windows.WToolkit
file.encoding.pkg=sun.io
java.version=1.1.1
file.separator=\
line.separator=

user.region=US
file.encoding=8859_1
java.vendor=Sun Microsystems Inc.
user.timezone=CST
user.name=Baldwin
os.arch=x86
os.name=Windows 95
java.vendor.url=http://www.sun.com/
user.dir=C:\BALDWIN\Cis2103J\Fall97\SampProg
java.class.path=;C:\Program Files\Kawa;C:\JAVA_JDK\JA...
java.class.version=45.3
os.version=4.0
path.separator=;
user.home=C:\JAVA_JDK\JAVA\BIN\..
**********************************************************/
import java.util.*;
import java.lang.*;

class SampProg27{
  public static void main(String[] args){
    //Instantiate and display a Properties object 
    // containing the system properties
    Properties obj = 
      new Properties(System.getProperties());
    obj.list(System.out);
  }//end main()
}//end class SampProg27

Q - Write a program that meets the following specifications.

/*File SampProg28.java from lesson 52
Copyright 1997, R.G.Baldwin
Without viewing the solution that follows, write a Java
application that illustrates the use of Java command-line
arguments.

When this program is run from the command line as follows:

java SampProg28 My command line arguments

the program should produce the following output:

My
command
line
arguments
**********************************************************/
class SampProg28 { //define the controlling class
  public static void main(String[] args){ //define main
    for(int i=0; i < args.length; i++)
    System.out.println( args[i] );
  }//end main
}//End SampProg28 class.  Note no semicolon required

//End Java application

-end-