The biggest advantage of writing an application in the Java language is "compile once, run everywhere," but that's not to say that all Java programs have cross-platform features, and in fact, quite a bit of Java programs don't work properly on other operating systems, So how do you write a real cross-platform Java program? Here are some things to be aware of when writing cross-platform Java programs:
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, and so on, but be aware that your Java program can only use Java Core API packages, If you want to use 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-independent features.
2. Regardless of whether you are using JDK or other development tools, all warning options are turned on at compile time, so that the compiler can find as many platform-related statements as possible and give warnings. While it is not guaranteed that programs without compile-time warning errors must be cross-platform, programs that contain warning errors are likely to be platform-independent.
3. When using any method in a program, look at the document in detail to make sure that the method you are using is not a method that has been declared obsolete in the document (deprecated methods), nor is it an implied method (undocumented) that is not marked in the document.
4. Try not to use the Java.lang.System exit method when exiting Java programs. The exit method terminates the JVM, terminating the program, but if you run another Java program at the same time, using the Exit method will cause the program to shut down, which is clearly not what we want to see. In fact, to exit the Java program, you can use Destory () to exit an independently running process. For multithreaded programs, you must close individual non-daemon threads. Use the Exit method to exit the program only if the program exits normally.
5. Avoid using local methods and local code, and rewrite the method as much as possible by writing Java classes that have the appropriate functionality. If you must use this local method, you can write a server program to call the method, and then either use the program you are writing now as the client for that server program, or consider CORBA (Common Object request Broker) program structure.
6.Java has a method similar to the 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 invoked to the method are platform-dependent, so avoid using it when writing programs, and if you must invoke other programs, you have to let the user set the command and its parameters themselves. For example, in Windows can call the Notepad.exe program, in Linux will be called VI program.
7. All information in the programming is to use the ASCII character set, because not all operating systems support the Unicode character set, which is a bad news for cross-platform Java Chinese software programs.
8. Do not rigidly encode any of the constants associated with the platform in the program, such as line separators, file separators, path separators, and so on, these constants are different on different platforms, such as file separators, in Unix and Macs are "/", 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 separator, GetProperty ("Line.separator") returns the row delimiter, GetProperty ("Path.separator") returns the path separator.
9. When writing a cross-platform network program, Do not use the Java.net.InetAddress class GetHostName method to get the hostname, because the different platform hostname format is different, it is best to use getaddress to get the same format IP address, in addition, all the host names in the program to be replaced by IP address, such as WWW.263.N ET will be replaced by the corresponding IP address.
10. Procedures involving file operations need to be noted: Do not hard-code the file path in the program, as in the case of 8, but this is particularly important and therefore is presented separately. Also, different platforms for file names using the characters and the maximum length of the file name is different, write your program to use the general ASCII characters as the name of the file, and can not be the same as the platform already exist, otherwise it will cause conflicts.
11. If you write programs that are GUI programs, you cannot rigidly set the size and location of components when using AWT components, and you should use the Java Layout Manager (layout manager) to set up and manage the size and location of visual components, otherwise it may cause layout confusion.
12. How do you get these properties because of different operating systems, different machines, and the color and screen sizes and resolutions that the system supports? You can use the Java.awt.Systemcolor class to get the color you want, such as the inactivecaption of a class that is the background color of the active caption in the window's border, and the menu's background color. Getscreenresolution with Java.awt.Toolkit can display the resolution of the screen in pixels per inch. The getscreensize of the class can get the screen size (inches) and loadsystemcolors can list all the system colors.
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.