📄 chap02.html
字号:
<P>In addition to guaranteeing the smallest set of APIs, the Card Platform, which is targeted at SmartCards, uses only a subset of the full Java Virtual Machine instruction set. Only a subset of the features of the Java language are supported by this smaller instruction set. As a result, only Java programs that restrict themselves to features available on the Card Platform can run on a SmartCard.</P>
<P>Because the Java Platform is compact, it can be implemented on a wide variety of embedded systems. The compactness of the Java Platform, however, does not restrict implementation at the opposite end of the spectrum. The Java Platform also scales up to personal computers, workstations, and mainframes.</P>
<H3><EM><P>Factors that Influence Platform Independence</P>
</EM></H3><P>When you write a Java program, its degree of platform independence depends on several factors. As a developer, some of these factors are beyond your control, but most are within your control. Primarily, the degree of platform independence of any Java program you write depends on how you write it.</P>
<H3><P>Java Platform Deployment</P>
</H3><P>The most basic factor determining the a Java program韘 platform independence is the extent to which the Java Platform has been deployed on multiple platforms. Java programs will only run on computers and devices that host a Java Platform. Thus, before one of your Java programs will run on a particular computer owned by, say, your friend Alicia, two things must happen. First, the Java Platform must be ported to Alicia韘 particular type of hardware and operating system. Once the port has been done by some Java Platform vendor, that port must in some way get installed on Alicia韘 computer. So a critical factor determining the true extent of platform independence of Java programs--and one that is beyond the control of the average developer--is the availability of Java Platform implementations and their distribution.</P>
<P>Fortunately for the Java developer, the deployment of the Java Platform has proceeded with great momentum, starting with Web-browsers, then moving on to desktop, workstation, and network operating systems. With the advent of chips optimized to execute Java bytecodes efficiently, the Java Platform will to some extent work its way into many different kinds of embedded devices. It is increasingly likely, therefore, that your friend Alicia will have a Java Platform implementation on her computer.</P>
<H3><P>The Java Platform Version and Edition</P>
</H3><P>The deployment of the Java Platform is a bit more complicated, however, because not all standard runtime libraries are guaranteed to be available at every Java Platform. The basic set of libraries guaranteed to be available at a Java Platform is called the <I>Java Core API</I>. A Java Virtual Machine accompanied by the class files that constitute the Core API is called the <I>Java Core Platform</I>. This edition of the Java Platform has the minimum set of Java API libraries that you can assume will be available at network computers, desktop computers, and workstations. As mentioned earlier, three other editions of the Java Platform--the Embedded, Personal, and Card Platforms--provide subsets of the Core API for embedded systems. The standard runtime libraries not guaranteed to be available at the Core Platform are collectively called the <I>Java Standard Extension API</I>. These libraries include such services as telephony, commerce, and media such as audio, video, or 3D. If your program uses libraries from the Standard Extension API, it will run anywhere those standard extension API libraries are available, but not on a computer that implements only the basic Java Core Platform.</P>
<P>Another complicating factor is that in a sense the Java Platform is a moving target--it evolves over time. Although the Java Virtual Machine is likely to evolve very gradually, the Java API will probably change more frequently. Over time, features will be added to and removed from both the Core and Standard Extension APIs, and parts of the Standard Extension API may migrate into the Core API. The changes made to the Java Platform should for the most part be upwards compatible, meaning they won韙 break existing Java programs, but some changes may not be. As obsolete features are removed in a new version of the Java Platform, existing Java programs that depend upon those features won韙 run on the new version. Also, changes may not be downwards compatible, meaning programs that are compiled for a new version of the Java Platform won韙 necessarily work on an old version. The dynamic nature of the Java Platform complicates things somewhat for the developer wishing to write a Java program that will run on any computer.</P>
<P>In theory, your program should run on all computers that host a Java Core Platform so long as you depend only upon the runtime libraries in the Core API. In practice, however, new versions of the Core API will take time to percolate everywhere. When your program depends on newly added features of the latest version of the Java Core API, there may be some hosts that can韙 run it because they have an older version. This is not a new problem to software developers--programs written for Windows 95, for example, don韙 work on the previous version of the operating system, Windows 3.1--but because Java enables the network delivery of software, it becomes a more acute problem. The promise of Java is not only that it is easy to port programs from one platform to another, but that one version of a binary executable Java program placed on a server can be delivered across a network and run on all computers. </P>
<P>As a developer, you can韙 control the release cycles or deployment schedules of the Java Platform, but you can choose the Java Platform edition and version that your programs depend upon. In practice, therefore, you will have to decide when a new version of the Java Platform has been distributed to a great enough extent to justify writing programs for that version.</P>
<H3><P>Native Methods</P>
</H3><P>Besides the Java Platform version and edition your program depends on, the other major factor determining the extent of platform independence of your Java program is whether or not you call native methods. The most important rule to follow when you are writing a platform independent Java program is: don韙 directly or indirectly invoke any native methods that aren韙 part of the Java API. As you can see in Figure 2-1, calling native methods outside the Java API renders your program platform-specific.</P>
<P><IMG SRC="fig2-1.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/images/fig2-1.gif" ALT="Figure 2-1"></P>
<P>Calling native methods directly is appropriate in situations where you don韙 desire platform independence. In general, native methods are useful in three cases:</P>
<UL><LI> for accessing features of an underlying host platform that are not accessible through the Java API
<LI> for accessing a legacy system or using an already existing library that isn韙 written in Java
<LI> for speeding up the performance of a program by implementing time-critical code as native methods</UL>
<P>If you need to use native methods and also need your program to run on several platforms, you韑l have to port the native methods to all the required platforms. This porting must be done the old fashioned way, and once you韛e done it, you韑l have to figure out how to deliver the platform-specific native method libraries to the appropriate hosts. Because Java韘 architecture was designed to simplify multi-platform support, your initial goal in writing a platform-independent Java program should be to avoid native methods altogether and interact with the host only through the Java API.</P>
<H3><P>Non-Standard Runtime Libraries</P>
</H3><P>Native methods aren韙 inherently incompatible with platform independence. What韘 important is whether or not the methods you invoke are implemented "everywhere." Implementations of the Java API on operating systems such as Windows or Solaris use native methods to access the host. When you call a method in the Java API, you are certain it will be available everywhere. It doesn韙 matter if in some places the method is implemented as a native method.</P>
<P>Java Platform implementations can come from a variety of vendors, and although every vendor must supply the standard runtime libraries of the Java API, individual vendors may also supply extra libraries. If you are interested in platform independence, you must remain aware of whether any non-standard runtime libraries you use call native methods. Non-standard libraries that don韙 call native methods don韙 degrade your program韘 platform independence. Using non-standard libraries that do call native methods, however, yields the same result as calling native methods directly--it renders your program platform-specific.</P>
<P>For example, Microsoft offers several sets of non-standard runtime libraries with their Java Platform implementation. One set is the Application Foundation Classes (or AFC). The AFC library is delivered along with the Java Platform implementation in Microsoft韘 Internet Explorer (Version 4.0 and beyond) web browser. The AFC library extends the capabilities provided by the standard runtime libraries of the Java API, but doesn韙 call native methods outside the Java API. The AFC library accesses the host only through the Java API. As a result, programs that use AFC should run on all implementations of the Java Platform.</P>
<P>One concern surrounding the use of a non-standard class library (such as AFC) that doesn韙 call native methods, is you must deliver it to Java Platforms that don韙 support it directly. For instance, imagine you write a Java program that uses AFC and make it available for download across a network. If you want the program to run on all platforms, you韑l have to make the AFC classes available for download too. Compared to other browsers, the program might get started sooner at Microsoft韘 browser because it already has the AFC classes close at hand. Other browsers that don韙 store the AFC library locally would have to download the needed AFC classes across the network. Other than this potential difference in download time, using a non-standard library that interacts with the host only through the Java API won韙 reduce the platform independence of a Java program. To keep your program platform independent, however, you must deliver the library along with your program.</P>
<P>One other Microsoft extension to the standard libraries of the Java Platform gives you a way to generate Java class files that grant access to COM (Component Object Model) objects. If you want to interact with a particular COM object, you use Microsoft韘 tool to generate a Java class file that gives your program a Java interface to the COM object. In effect, the generated class files form an extra runtime library that is available only at Microsoft韘 Java Platform implementation. Although this extra library offers more capabilities to Java programs, it reduces the platform independence of any Java program that takes advantage of it. Why? Because the extra libraries call native methods that are, initially at least, only available on Windows 95 and Windows NT. (In the future, Microsoft plans to port the Java/COM interface to non-Microsoft platforms.) Because the extra libraries provided by Microsoft use native methods that are specific to Microsoft operating systems, Java programs that use the extra libraries will only work on Microsoft operating systems.</P>
<P>Another potential ramification of using a vendor韘 non-standard runtime library that calls native methods directly is that your program will only work on that vendor韘 Java Platform implementation. For example, the class files described above that give your Java program access to COM objects use a special native method interface (the Java/COM interface) of Microsoft韘 Java Virtual Machine. Currently, this special native method interface is available only on Microsoft韘 virtual machine implementation on Windows 95 and Windows NT. Microsoft may port the Java/COM interface to other platforms, to increase the platform independence of programs that take advantage of it. But even if Microsoft is able to port it to every major platform, Java programs that use it will most likely only work properly when running on a Microsoft implementation of the Java Virtual Machine.</P>
<P>Because Java Platforms can come from different vendors, there can be different Java Platform implementations from different vendors for the same hardware and operating system. To run a Java program on Windows 95, for example, you could use a Java Platform from Sun, Microsoft, Borland, Symantec, or Asymetrix. The level of platform independence that a Java program has depends not only on how many different host computers it can run on, but also on how many different Java Platform implementations it can run on each host.</P>
<H3><P>Virtual Machine Dependencies</P>
</H3><P>Two other rules to follow when writing a platform independent Java program involve portions of the Java Virtual Machine that can be implemented differently by different vendors. The rules are:</P>
<OL><LI>don韙 depend upon timely finalization for program correctness, and</P>
<LI>don韙 depend upon thread prioritization for program correctness.</OL>
<P>These two rules address the variations allowed in the Java Virtual Machine specification for garbage collection and threads.</P>
<P>All Java Virtual Machine must have a garbage-collected heap, but different implementations can use different garbage collection techniques. This flexibility in the Java Virtual Machine specification means that the objects of a particular Java program can be garbage collected at completely different times on different virtual machines. This in turn means that finalizers, which are run by the garbage collector before an object is freed, can run at different times on different virtual machines. If you use a finalizer to free finite memory resources, such as file handles, your program may run on some virtual machine implementations but not others. On some implementations, your program could run out of the finite resource before the garbage collector gets around to invoking the finalizers that free the resource.</P>
<P>Another variation allowed in different implementations of the Java Virtual Machine involves thread prioritization. The Java Virtual Machine specification guarantees that all runnable threads at the highest priority in your program will get some CPU time. The specification also guarantees that lower priority threads will run when higher priority threads are blocked. The specification does not, however, prohibit lower priority threads from running when higher priority threads aren韙 blocked. On some virtual machine implementations, therefore, lower priority threads may get some CPU time even when the higher priority threads aren韙 blocked. If your program depends for correctness on this behavior, however, it may work on some virtual machine implementations but not others. To keep your multi-threaded Java program platform independent, you must rely on synchronization--not prioritization--to coordinate inter-activity between threads.</P>
<H3><P>User Interface Dependencies</P>
</H3><P>Another major variation between different Java Platform implementations is user interface. User interface is one of the more difficult issues in writing platform independent Java programs. The AWT user interface library gives you a set of basic user-interface components that map to native components on each platform. Libraries such as Microsoft韘 AFC, Netscape韘 IFC, and Sun韘 JFC, give you advanced components that don韙 map directly to native components. From this raw material, you must build an interface that users on many different platforms will feel comfortable with. This is not always an easy task.</P>
<P>Users on different platforms are accustomed to different ways of interacting with their computer. They metaphors are different. The components are different. The interaction between the components is different. Although the AWT library makes it fairly easy to create a user interface that runs on multiple platforms, it doesn韙 necessarily make it easy to devise an interface that keeps users happy on multiple platforms.</P>
<H3><P>Bugs in Java Platform Implementations</P>
</H3><P>One final source of variation among different implementations of the Java Platform is bugs. Although Sun has developed a comprehensive suite of tests that Java Platform implementations must pass, it is still possible that some implementations will be distributed with bugs in them. The only way you can defend yourself against this possibility is through testing. If there is a bug, you can determine through testing whether the bug affects your program, and if so, attempt to find a work-around.</P>
<H3><P>Testing</P>
</H3><P>Given the allowable differences between Java Platform implementations, the platform dependent ways you can potentially write a Java program, and the simple possibility of bugs in any particular Java Platform implementation, you should test your Java programs on all platforms you plan to claim it runs on. Java programs are not platform independent to a great enough extent that you only need test them on one platform. You still need to test a Java program on multiple platforms, and you should probably test it on the various Java Platform implementations that are likely to be found on each host computer you claim your program runs on. In practice, therefore, testing your Java program on the various host computers and Java Platform implementations that you plan to claim your program works on is a key factor in making your program platform independent.</P>
<H3><EM><P>Seven Steps to Platform Independence</P>
</EM></H3><P>Java韘 architecture allows you to choose between platform independence and other concerns. You make your choice by the way in which you write your program. If your goal is to take advantage of platform-specific features not available through the Java API, to interact with a legacy system, to use an existing library written not written in Java, or to maximize the execution speed of your program, you can use native methods to help you achieve that goal. In such cases, your programs will have reduced platform independence, and that will usually be acceptable. If, on the other hand, your goal is platform independence, then you should follow certain rules when writing your program. The following seven steps outline one path you can take to maximize your program韘 portability:</P>
<OL><LI>Choose a set of host computers that you will claim your program runs on (your "target hosts").</P>
<LI>Choose a version of the Java Platform that you feel is well enough distributed among your target hosts. Write your program to run on this version of the Java Platform.</P>
<LI>For each target host, choose a set of Java Platform implementations that you will claim your program runs on (your "target runtimes").</P>
<LI>Write your program so that it accesses the host computer only through the standard runtime libraries of the Java API. (Don韙 invoke native methods, or use vendor-specific libraries that invoke native methods.)</P>
<LI>Write your program so that it doesn韙 depend for correctness on timely finalization by the garbage collector or on thread prioritization.</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -