Article Source: Computer World Daily
Although this article is old, some places are still worth reference.
-------------------------------------
The biggest advantage of using the Java language to write applications is "Run everywhere", this is not to say that all Java programs have cross-platform features. In fact, a considerable part of Java programs cannot be on other operating systems. Correctly run, so how can you write a real cross-platform Java program? Below is some things you need to pay attention to writing cross-platform Java programs:
1. When writing Java cross-platform applications, you can choose JDK1.0, 1.1, 1.2 or supports their GUI development tools such as JBuilder, Visual Age for Java, etc., you must pay attention to your Java program only use Java core. API package, if you want to use a third-party class library package, the library package is also done by the Java core package, otherwise you have to release the JVM of the Java class library package when publish your program. That is, your program needs to be 100% pure Java. For example, Visual J is not pure Java, and programs written by Visual J do not have platform irrelevant.
2. Whether you are using a JDK or other development tool, all warning options are opened when compiling so that the compiler can discover the platform-related statements as much as possible and give a warning. While not guarantees that the warning error is not compiled, it must be a cross-platform, but the program containing a warning error is very likely to be non-platform.
3. When using any method in the program, you must see the document in detail, make sure that the method you use is not a way to expand it in the document, nor the unmet implicit method in the document (undocument method ).
4. Try not to use Java.lang.system's Exit method when exiting the Java program. The exit method can terminate the JVM, thus terminating the program, but if another Java program is running, using the exit method will also turn the program close, which is obviously not what we want to see. In fact to exit Java programs, you can use Destory () to exit a separate running process. For multi-threaded programs, you must turn off each non-daemon thread. The exit method exits the program only when the program is absent from normal exit.
5. Avoid using local methods and local code, and write the Java classes with the corresponding functions as much as possible to change the method. If you must use the local method, you can write a server program to call the method, then the program you want to write as the client program of the server, or consider the CORBA (Public Object Request Agent) program structure.
6. Java has a method similar to the WINEXEC in Delphi, the exec method of the java.lang.Runtime class, as the method itself has a platform-independent, but the command and command parameters called by the method are and the platform Related, so, avoid use when writing a program, if you must call other programs, you must let the user to set the command and its parameters. For example, you can call the NotePad.exe program in Windows, and the VI program is called in Linux. 7. All information in the programming is used to use the ASCII code character set because all operating systems support Unicode character sets, which cannot be a big bad news for cross-platform Java Chinese software programs.
8. Do not hardly encode any constants related to the platform, such as linear dividers, file separators, path separators, etc., such as file separators, in Unix and Mac. It is "/" in Windows, "/" in Windows, if you want to use these constants, you need to use the GetProperty method for the JDAVA.UTIL.PROPERTIES class, such as java.util.properties.getProperty ("file.separator") Separator, GetProperty ("line.separator") returns a row separator, and getProperty ("path.separator") returns the path separator.
9. When writing cross-platform network programs, don't get the hostname of the Java.Net.Netaddress class, because the host name format of different platforms is different, it is best to get the same IP address in the format using GetDress, and All hostnames in the program have to be replaced with IP addresses, such as www.263.net, to replace the corresponding IP address.
10. Programs relating to file operations require attention: Do not use hard coded file paths in the program, just as in 8, just this is particularly important, so separately. Moreover, different platforms are different from the requirements of the characters and maximum file names used by the file name. When writing your program, use the general ASCII code character as the name of the file, and cannot be with the program already existing in the platform, otherwise it will Conflict.
11. If you write the program is a GUI program, you should use the size and location of the components when using the AWT component, and you should use the Java Layout Manager to set and manage the size and location of the visual components, otherwise May cause layout chaos.
12. How to get these attributes due to different operating systems, different machines, systems supported and the size of the screen, how to get these properties? Using the Java.awt.SystemColor class to get the colors, such as this class is the background color of the active title in the window border, Menu is the background color of the menu. GetScreenResolution using java.awt.toolkit can display the resolution of the screen in "pixel per inch". The getScreensize of this class can get the screen size (inches), and LoadSystemColors can list all system colors.