Java Reference
In-Depth Information
This method returns an instance of Process that represents the external program.
This class provides access to the standard in , out , and err streams with which
the Java program can communicate with the external program. In steps 3 and
4, the Process object is used to read the output from the external process.
Another useful method on Process is the waitFor() method, which can be
used to force the calling Java thread to wait until the external process terminates.
Note that running external programs clearly involves the details of the par-
ticular host platform and OS. For example, the exec() method does not use a
shell. If a shell is needed, then it can be run directly [1].
Running an external program also obviously limits portability. However, in
cases where the external program is available on multiple platforms, such as for
web browsers, the application could use the system properties (Section 23.2) to
determine the platform and then use this information to select a platform specific
command.
23.4 Port communications
Many platforms provide a standard serial communications port. A Java appli-
cation can use a serial port to monitor, control, and record data from external
devices. For example, a remote weather station might have several sensors con-
nected to a PC via serial ports. A control program on the PC could collect the
data and then use a serial line connection to a modem to dial up a central station
and transmit the latest sensor data (or answer a central station that polls remote
stations by phone). The javax.comm package provides the essential tools to do
these tasks. It also supports the parallel port, though this type of port is becoming
much less common.
Unfortunately, javax.comm does not come as a standard part of the J2SE but
as an optional package . This means that it is available for several platforms but
not for every platform for which a JVM exists. Sun offers versions for Windows
and Solaris, and some independent sources provide it for Linux and other plat-
forms [2,3]. Currently there is no standard extension package for other types of
communications ports, though some independent sources provide classes to work
with the USB port on some platforms [4].
To run the programs discussed here, you will need to download the
javax.comm set of files to your computer and install them with you JDK
files. Instructions are included and we also offer some tips in the Web Course
Chapter 23.
We first give an overview of port communications in general and then look
specifically at serial port I/O. Although the USB and other faster ports are becom-
ing more popular for desktop peripherals than parallel and serial ports, we expect
that the RS-232 serial port will remain common on many devices for many years.
The topics Java I/O by Elliotte R. Harold [5] and Java Cookboo kbyIanF.
Darwin [6] are popular references for the javax.comm package. We refer the
Search WWH ::




Custom Search