Considerations for writing cross-platform Java programs

Source: Internet
Author: User
Tags terminates

The biggest advantage of writing applications in the Java language is "compile once, run everywhere", but this is not to say that all Java programs have cross-platform features, and in fact, quite a few Java programs do not operate correctly on other operating systems. So how can you write a truly cross-platform Java program? Here are some things to be aware of when writing a cross-platform Java program:

1. When writing Java cross-platform applications, 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 be aware that your Java program can only use the Java Core API package, If you are using a third-party class library package, the class library package will also be developed by the Java core package, or you will have to publish the JVM that supports the Java class Library package when you publish your program. In other words, 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 independence.

2. Regardless of whether you are using a JDK or other development tool, all warning options are turned on at compile time so that the compiler can discover as many platform-related statements as possible and give warnings. Although it is not guaranteed that a program that does not have a compile-time warning error must be cross-platform, the program that contains the warning error is likely to be non-platform independent.

3. When using any method in a program, look at the document in detail to make sure that you are using a method that is not already declared obsolete in the document (Deprecated method), or an implied method (undocumented) that is not indicated in the document.

4. Try not to use the Java.lang.System exit method when exiting the Java program. The exit method terminates the JVM and terminates the program, but if you run another Java program at the same time, using the Exit method will cause the program to close, which is obviously not what we want to see. In fact, to quit a Java program, you can use Destory () to exit a standalone run. For multithreaded programs, you must close each non-daemon thread. Exit the program using the Exit method only if the program exits abnormally.

5. Avoid using local methods and local code, and write your own Java class with the appropriate functionality, overwriting the method. If you must use this local method, you can write a server program to call the method, and then use the program that you are writing now as the client for that server program, or consider the CORBA (public object request proxy) program structure.

6. Java has a method similar to WinExec in Delphi, The Exec method of the Java.lang.runtime class, as the method itself is platform-independent, but the command and command parameters called to the method are platform-dependent, so in writing the program to avoid use, if you must call other programs, you must let the user set the command and its parameters. For example, the Notepad.exe program can be called in Windows, and the VI program will be called in Linux.

7. The ASCII character set is used in all the information in the programming, because not all operating systems support the Unicode character set, which is not a bad news for a cross-platform Java software program.

8. In the program, do not hard code any of the platform-related constants, such as line separators, file separators, path separators, and so on, these constants are different on different platforms, such as the file delimiter, in Unix and Mac is "/", in Windows is "\", 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 the file delimiter, GetProperty ("Line.separator") returns the line delimiter, GetProperty ("Path.separator") returns the path delimiter.

9. When you write a cross-platform network program, Do not use the Java.net.InetAddress class GetHostName method to get the hostname, because different platform hostname format is different, it is best to use getaddress to get the same format IP address, in addition, all the host name in the program to be replaced by an IP address.

10. Procedures involving file operations need to be aware of: Do not hard code the file path in the program, the reason and the same as in 8, but this is particularly important, and therefore separately proposed. Moreover, different platforms for file names use the characters and the maximum file name length requirements are different, write your program to use the normal ASCII character as the name of the file, and can not be the same as the existing program in the platform, or it will cause conflicts.

11. If you write a program that is a GUI program, you cannot rigidly set the size and location of components while using the AWT components, and you should use the layout manager of Java to set and manage the size and position of the visual components, which could cause confusion in the layout.

12. How do you get these properties because of different operating systems, different machines, colors and screen sizes and resolutions that are supported by the system? Use the Java.awt.Systemcolor class to get the desired color, such as the inactivecaption of the class is the background color of the active caption in the window border, and menu is the background color of the menus. Using Java.awt.Toolkit's getscreenresolution, you can display the screen resolution in pixels per inch. The getscreensize of this class can get the screen size (inches), loadsystemcolors can list all the system colors.

Considerations for writing cross-platform Java programs

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.