Java Reference
In-Depth Information
Chapter 23
Accessing the platform
23.1 Escaping the sandbox
The Java Virtual Machine (JVM) is often said to provide a safe and self-contained
sandbox where programs such as applets can play without accidentally or delib-
erately entering restricted areas of the platform. That is sufficient if all a program
does is interact with the user via a graphical interface but many programs need
to reach out and access the world beyond the JVM to obtain information and
interact with external hardware.
In Chapter 22 we showed how Java classes can link to native codes, which
possess none of Java's security restrictions. This is the ultimate form of local
platform access but it involves a lot of inelegant coding and violates the portability
of Java. In this chapter we look at less drastic ways that a program can access
the platform. We first show how a program can obtain properties describing the
platform such as the operating system, the Java version, and screen size. We then
explain how to run a non-Java program from within a Java program. Next we
discuss how to use serial ports to communicate with external devices. We include
a demonstration program in which a Java application communicates via a serial
port with a temperature sensor.
We note that the security restrictions put into place by a browser JVM place
severe limits on the access that applets have to the platform and the network.
However, as we saw in Chapter 14, the security restrictions on Java applications
can be easily customized as needed. (We don't have space here to discuss trusted
applets that use digital signatures to authenticate their identity. Trusted applets
can be given access similar to that for applications.)
23.2 Accessing system properties
The class java.util.Properties is a subclass of Hashtable ,which
we discussed in Chapter 10. Java provides Properties tables with various
system settings in key/value string pairs. See the Java API Specification for
the System.getProperties() method for a listing of the keys, some of
which include java.version , java.home , os.name , user.name , user.
home , etc.
625
Search WWH ::




Custom Search