Portability Conventions and Pure Java Rules
Sun’s motto, or core value proposition, for Java is “Write once, run anywhere.” Java makes it easy to write portable programs, but Java programs do not automatically run successfully on any Java platform. The following tips help to avoid portability problems. Portability rules like those listed here were the focus of Sun’s now-defunct “100% Pure Java” certification program and branding campaign.
- Native methods
Portable Java code can use any methods in the core Java APIs, including methods implemented as
native
methods. However, portable code must not define its own native methods. By their very nature, native methods must be ported to each new platform, so they directly subvert the “Write once, run anywhere” promise of Java.- The
Runtime.exec( )
method Calling the
Runtime.exec( )
method to spawn a process and execute an external command on the native system is rarely allowed in portable code. This is because the native OS command to be executed is never guaranteed to exist or behave the same way on all platforms. The only time it is legal to useRuntime.exec( )
is when the user is allowed to specify the command to run, either by typing the command at runtime or by specifying the command in a configuration file or preferences dialog box.- The
System.getenv()
method Using
System.getenv()
is nonportable. The method was deprecated but has been reintroduced in Java 5.0.- Undocumented classes
Portable Java code must use only classes and interfaces ...
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.