The biggest advantage of writing applications with Java language is "Run everywhere", this is not to say that all Java programs have cross-platform features, in fact, a considerable part of the Java program cannot be in 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 support their GUI development tools such as JBuilder, Visual Age for Java, etc., but 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 will also be completed by the Java core package, otherwise you have to release the JVM of the Java class library package when you release 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 Method in the document, nor the Idocunted Method, which is not indicated in the document.
4. Try not to use java.lang.system's exit method when you exit Java programs. 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 codes, and write the Java class with the corresponding function as much as possible to rewrite 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. There is a method similar to the WINEXEC in Delphi, the exec method of the java.lang.Runtime class, as the method itself is unrelated, but the command and command parameters called by the method are related to the platform. Therefore, when writing a program, avoid use, 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 programming is used to use the ASCII code character set, because all operating systems support Unicode character set, which cannot be a big news for cross-platform Java Chinese software programs.
8. Do not hardly encodes any constants associated with the platform, such as line separators, file separators, path separators, and the like, such as file separators, in UNIX and MACs. "/", "/" In Windows, if you want to use these constants, you need to use the getProperty method of the JDAVA.UTIL.PROPERTIES class, such as java.util.properties.getProperty ("file.separator") to get file separator GetProperty ("line.separator") Returns the line separator, getProperty ("path.separator") returns the path separator. 9. When writing cross-platform network programs, don't get the host name, 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 the program All hostnames must be replaced into IP addresses, such as
Www.263.net is replaced with 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 especially 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 the program you write 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 up the size and location of the visual components, otherwise it is possible to cause The layout is chaotic.
12. How to get these attributes due to different operating systems, different machines, systems supported by different machines, and the size and resolution of the screen, how to get these attributes? 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.
These precautions are from references, and some are their experience in writing Java programs for a long time. I believe it will help your programming.