Java Reference
In-Depth Information
It's important to distinguish between testing this at compile time and at runtime. In both
cases, this code must be compiled on a system that includes the classes you are testing
for—JDK >= 1.1 and Swing, respectively. These tests are only attempts to help the poor
backwater Java runtime user trying to run your up-to-date application. The goal is to provide
this user with a message more meaningful than the simple “class not found” error that the
runtime gives. It's also important to note that this test becomes unreachable if you write it in-
side any code that depends on the code you are testing for. The check for Swing won't ever
see the light of day on a JDK 1.0 system if you write it in the constructor of a JPanel sub-
class (think about it). Put the test early in the main flow of your application, before any GUI
objects are constructed. Otherwise the code just sits there wasting space on newer runtimes
and never gets run on Java 1.0 systems. Obviously this is a very early example, but you can
use the same technique to test for any runtime feature added at any stage of Java's evolution
(see Appendix A for an outline of the features added in each release of Java). You can also
use this technique to determine whether a needed third-party library has been successfully
added to your classpath.
As for what the class Class actually does, we'll defer that until Chapter 23 .
Dealing with Operating System-Dependent Variations
Problem
You need to write code that adapts to the underlying operating system.
Solution
You can use System.Properties to find out the operating system, and various features in
the File class to find out some platform-dependent features.
Discussion
Though Java is designed to be portable, some things aren't. These include such variables as
the filename separator. Everybody on Unix knows that the filename separator is a slash char-
acter (/) and that a backward slash, or backslash (\), is an escape character. Back in the late
1970s, a group at Microsoft was actually working on Unix—their version was called Xenix,
later taken over by SCO—and the people working on DOS saw and liked the Unix filesys-
Search WWH ::




Custom Search