Cross-platform Java project_mysql

Source: Internet
Author: User
Tags file separator
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 is a cross-platform Java program. The biggest advantage of using Java to compile 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, so 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 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, 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.

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