Notes for compiling cross-platform Java programs

Source: Internet
Author: User
Tags file separator
Notes for compiling cross-platform Java programs-general Linux technology-Linux programming and kernel information. For details, refer to the following. The biggest advantage of using Java to write an application is "one-time compilation and running everywhere". However, this does not mean that all Java programs have cross-platform features. In fact, A considerable number of Java programs cannot run correctly on other operating systems. How can we compile a real cross-platform Java program? The following are some notes for compiling cross-platform Java programs:
1. when writing Java cross-platform applications, you can choose JDK1.0, 1.1, 1.2 or other GUI development tools that support them, such as Jbuilder and Visual Age for Java, however, you must note that your Java program can only use the Java core API package. If you want to use a third-party class library package, the class library package must also be developed by the Java core package, otherwise, you must release the JVM that supports the Java class library package when releasing your program. That is to say, your program needs to be 100% pure Java. For example, Visual J ++ is not pure Java, and programs written by Visual J ++ are not platform-independent.
2. Whether you are using JDK or other development tools, you must enable all warning options during compilation, so that the compiler can discover as many platform-related statements as possible and give warnings. Although it cannot be guaranteed that programs without warning errors during compilation must be cross-platform, programs with warning errors may be non-platform independent.
3. when using any method in a program, check the document in detail to make sure that you are not using a Deprecated method that has been declared as obsolete in the document ), it is not an unauthenticated ented method that is not specified in the document ).
4. Do not use the exit method of Java. lang. System when exiting the java program. The Exit method can terminate the JVM and terminate the program. However, if another Java program is run at the same time, the exit method will disable the program. This is obviously not what we want to see. In fact, to exit the Java program, you can use destory () to exit an independent process. For multi-threaded programs, all non-daemon threads must be closed. Exit is used only when the program exits abnormally.
5. Avoid using local methods and local code, write Java classes with corresponding functions as much as possible, and rewrite this method. If you must use this local method, you can write a server program to call this method, and then use the program to be compiled as the client program of this server program, or consider the structure of the Common Object Request proxy program.
6. java has a method similar to winexec in Delphi, java. lang. the exec method of the runtime class is platform-independent, but the commands and command parameters for the method are platform-related. Therefore, avoid using this command when writing a program. If you must call other programs, you must set the command and its parameters by yourself. For example, you can use the notepad.exe program in windows. In linux, you need to call the vi Program.
7. All information in the program design must use the ASCII character set, because not all operating systems support the Unicode character set. This is a big waste for cross-platform Java Chinese software programs.
8. in a program, do not hard encode any constants related to the platform, such as line separators, file delimiters, and path delimiters. These constants are different on different platforms, such as file delimiters, it is "/" in UNIX and MAC, and "in windows. To use these constants, you need to use jdava. util. the getProperty method of the Properties class, such as java. util. properties. getProperty ("file. "separator") to get the file separator, getProperty ("line. "separator") returns the line separator, getProperty ("path. ") returns the path separator.
9. when compiling cross-platform network programs, do not use java.net. the getHostName method of the InetAddress class obtains the host name. Because the host name formats of different platforms are different, it is best to use getAddress to obtain the IP address in the same format. In addition, all host names in the program must be replaced with the IP address, for example, www.263.net must be replaced with the corresponding IP address.
10. For programs involving file operations, Note: Do not hard-code the file path in the program. The reason is the same as that in 8, but this is especially important. Therefore, it is proposed separately. In addition, different platforms have different requirements on the Characters Used for file names and the maximum length of file names. When writing your program, you must use common ASCII characters as the file name, and cannot have the same name as an existing program on the platform. Otherwise, a conflict may occur.
11. if the program you write is a GUI program, you should use the layout manager of Java instead of setting the size and position of the component hard when using the AWT component) to set and manage the size and position of the visual component. Otherwise, the layout may be messy.
12. Since different operating systems and machines support different colors and screen sizes and resolutions, how can we obtain these attributes? You can use the java. awt. Systemcolor class to obtain the required color. For example, inactiveCaption of this class is the background color of the Activity title in the window border, and menu is the background color of the menu. The getScreenResolution of java. awt. Toolkit can display the screen resolution in pixels per inch. The getScreenSize of this class can get the screen size (INCHES), and loadSystemColors can list all system colors.
Some of these considerations come from references, and some are from your long-term experience in writing Java programs. I believe they will be helpful for your program design.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.