Senior JAVA programming: cross-platform programs written note



Write applications using the Java language is the biggest advantage of "a compilation, run everywhere", but this does not mean that all Java programs are cross-platform features, in fact, a considerable part of the Java program is not in the other operating systemscorrectly, then how can write a truly cross-platform Java program?The following is the preparation of cross-platform Java programs are some of the things to note:

Preparation of cross-platform Java application, you can choose to 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 can only use the Java core API packagesIf you want to use third-party library package, the library should package developed by the Java core packages, or procedures for the release when you have the support of the Java class library packages JVM publish.In other words, your program needs to be 100% pure Java.As an example, Visual J + + is not pure Java, the Visual J + + program can not be written in platform-independent.

Whether you are using a JDK or other development tools, at compile time options should be open all the warnings, so the compiler can be as much as possible the statement that platform, and gives a warning.Although there is no compile-time warnings can not guarantee that the error must be cross-platform program, but the procedure is very wrong with warnings may be non-platform-independent.

In the program when using any one method, to look into the document, make sure you use the method is not stated in the document is outdated methods have been (Deprecated method), nor is it implicit in the document does not indicate the method (Undocumented method).

Try not to exit the Java program's exit method to use java.lang.System.Exit ways to terminate the JVM, to terminate the program, but if run another Java program, use exit method will allow the program has been closed, which is obviously not what we want to see.To exit the Java program in fact, you can use destory () exit a stand-alone process.For multi-threaded programs, we must turn off all non-daemon thread.Only when the program exited only way to exit the program using the exit.

Avoid using local methods and local codes, as far as possible to write your own functions with the corresponding Java class, override this method.If you must use the native methods, you can write a server program calls the method, and then the program will now write the server program as a client program, or consider the CORBA (Common Object Request Broker) program structure.

Java, Delphi in a similar way winexec, java.lang.runtime class exec method as the method itself is independent of platform, but the method call to the command and command parameters are associated with the platformTherefore, in the preparation of programs to avoid, if we want to call other programs, they must let the user to set the command and its parameters.For example, you can call notepad.exe in the windows procedure should call the vi in linux in the program.

Programming all of the information in the code have to use ASCII character set, because not all operating systems support Unicode character set, which for cross-platform Java programs can not be said of Chinese software is a big bad news.

Do not hard coded in the program, any constants associated with the platform, such as the line separator, file separator, path separator, etc., in these constants are different on different platforms, such as file separator, is in the UNIX and MAC"/" in windows is "", if you want to use these constants, you need jdava.util.Properties class getProperty methods, such as java.util.Properties.getProperty ("file.separator") can get file separator,getProperty ("line.separator") returns the line separator, getProperty ("path.separator") return path separator.

In the preparation of cross-platform web applications, do not use java.net.InetAddress class getHostName obtained host name, because of the different platforms of different host name format is best to use the getAddress get the same IP address format, the other, the programAll the host name should be replaced by IP address, such as www.javafan.net necessary to replace the corresponding IP address.

Procedures related to file operations Note: Do not hard coded in the program file path, and 8, the same reasons, but this is particularly important, so separately.Moreover, the different platforms used for file name characters and the maximum file name length requirements are different, the time to write your program to use ASCII code characters as the general name of the file and can not be a platform the same name already exists in the program, otherwise it willconflict.

If your program is a GUI program written in the use of AWT components can not be arbitrarily set the size and location of the component should use Java's layout manager (layout manager) to set up and manage the size and location of visual components, orthe layout may cause confusion.

Because different operating systems, different machines, the system supports color and screen size and resolution are different, how to obtain these attributes?Class can get the required use of java.awt.Systemcolor color, such as class inactiveCaption event title in the window frame is the background color, menu is the menu background color.The getScreenResolution can use java.awt.Toolkit "pixels per inch" as units of the display screen resolution.GetScreenSize class can get the screen size (inch), loadSystemColors can list all the system colors.

Some of these considerations from the references, some of his long experience in writing Java programs, I believe your program will help design