662 Cross-Platform Programming entire program from scratch for a different operating system, but this process isn’t trivial. Create a program that can run on three different operating systems, and now you have to worry about maintaining and fixing problems with your program on three different operating systems. It’s possible for your program to work perfectly under Linux, but crash under Windows and the Macintosh. The more operating systems your program supports, the greater the complexity in getting your program to work right under all these different operating systems. Virtual machines The main advantage of cross-platform compilers is that they compile programs directly into machine language for each specific operating system, which makes your programs run as fast as possible. With today’s faster computers, speed is rarely as important as in the old days when slow programs could literally take hours to perform a single task. Because speed isn’t as crucial as in the past, computer scientists have created another way to create cross-platform programs known as virtual machines. Rather than compile a program for a specific processor, virtual machine programming languages compile programs to a generic format (called bytecode, pseudocode, or p-code) that can run on a virtual machine. This virtual machine, running on different operating systems, essentially tricks the program into thinking it’s running on a single computer, as shown in Figure 5-2. Source code Compiler Figure 5-2: A virtual machine lets a program run on multiple operating systems. Microsoft Windows Virtual Machine Mac OS X Linux
Cross-Platform Programming 663 The biggest problem with virtual machine programming languages, such as Java, is their lack of speed. Because virtual machine languages aren’t compiled, they run slower than true compiled programs. Even worse, virtual machine languages can run only on operating systems that have virtual machines written for them. Sun Microsystems, the creator of the Java programming language, has created Java virtual machines for all the major operating systems, including Windows, Macintosh, and Linux. To maintain this ability to run on multiple operating systems, Sun Microsystems must constantly update each virtual machine for each operating system. This added complexity earned Java its initial reputation as a “write once, debug everywhere” language — you had to fix your program on multiple operating systems. Despite this constant need to create and modify virtual machines for multiple operating systems, Java has proven capable of creating truly cross-platform programs. One company has even created a Microsoft Office clone, ThinkFree (www.thinkfree.com), which can run on Windows, Linux, and the Macintosh. Software as service As successful as ThinkFree and other virtual machine-based programs have been, the latest trend is toward selling software as a service. The idea is that instead of having a copy of a program stored on your computer, you use the Internet to access a program stored on another computer, or a server. Book VII Chapter 5 The main advantage of this software as service model is consistency. With programs written in C, cross-platform compilers (like REALbasic), or virtual machine languages (like Java), the end result is always a program stored on each person’s computer. Therefore, one person could be using version 1.0 of a program, another could be using version 1.4, and a third could be using version 1.57, which makes supporting all these different versions difficult. The Future of Computer Programming A second advantage of the software as service model is that it’s based on Internet Web browsing standards, so if a computer can access the Internet, it can use any software as service program. This gives you a cross-platform program without the hassle of making sure your program runs on each particular operating system. A third advantage of the software as service model is that it frees up space on your hard disk. Instead of cluttering your computer with dozens of separate programs, you only need to store your actual files on your computer because the software as service programs are stored on another computer.