[ Team LiB ] |
3.10 Compile to Native Machine CodeIf you know the target environments of your application, you have the option of taking your Java application and compiling it to a machine-code executable. A variety of these compilers target various platforms, and the list continues to grow. (Check the computer magazines or follow the compiler links on good Java web sites. See also the compilers listed in Chapter 19 and at http://www.JavaPerformanceTuning.com/resources.shtml.) These compilers often work directly from the bytecode (i.e., the .class files) without the source code, so any third-party classes and beans you use can normally be included. If you use this option, a standard technique to remain multiplatform is to start the application from a batch file that checks the platform and installs (or even starts) the application binary appropriate for that platform, falling back to the standard Java runtime if no binary is available. Of course, the batch file also needs to be multiplatform, but then you could build it in Java. Prepare to be disappointed with the performance of a natively compiled executable compared to the latest JIT-enabled runtime VMs. The compiled executable still needs to handle garbage collection, threads, exceptions, etc., all within the confines of the executable. These runtime features of Java do not necessarily compile efficiently into an executable. The performance of the executable may well depend on how much effort the compiler vendor has made in making those Java features run efficiently in the context of a natively compiled executable. The latest adaptive VMs have been shown to run some applications faster than the equivalent natively compiled executable. Advocates of the "compile to native executable" approach feel that the compiler optimizations will improve with time so that this approach will ultimately deliver the fastest applications. Luckily, this is a win-win situation for the performance of Java applications: try out both approaches if appropriate to you, and choose the one that works best. There are also several translators that convert Java programs into C . I only include a mention of these translators for completeness, as I have not tried any of them. They presumably enable you to use a standard C compiler to compile to a variety of target platforms. However, most source code-to-source code translations between programming languages are suboptimal and do not usually generate fast code. |