Programming experience: Notes for compiling cross-platform Java programs

Source: Internet
Author: User
Tags file separator
Programming experience: Notes for compiling cross-platform Java programs-general Linux technology-Linux programming and kernel information. The following is a detailed description. 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:

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.

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.

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 ).

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.

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.

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.

All information in the program design must use the ASCII character set, because not all operating systems support the Unicode character set, which is a big waste for cross-platform Java Chinese software programs.

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, "/" in UNIX and MAC and "\" in windows. To use these constants, 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.

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.javafan.net must be replaced with the corresponding IP address.

Note: Do not hard encode the file path in the program. The reason is the same as in 8, but this is especially important. 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.

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.

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.