How to write a cross-platform JAVA program?

zhaozj2021-02-16  52

The biggest advantage to write applications using Java language is "Run everywhere, run everywhere", but this is not to say

All Java programs have cross-platform features, in fact, a considerable part of the Java program cannot be exercised

How can I write a real cross-platform Java program? Here is

The Java program that writes the platform is some of the things you need to pay attention to:

1. When writing Java cross-platform applications, you can choose JDK1.0, 1.1, 1.2 or support for their GUI development.

Such as: JBuilder, Visual Age for Java, etc., but must pay attention to your Java program can only use JAV

a core API package, if you want to use a third-party class library package, the library package is also developed by Java core package, no

The JVM that supports the Java class library package will be released in the time of posting your program. That is, your program

Need to be 100% pure Java. For example, Visual J is not pure Java, written by Visual J

The program does not have a platform-independent.

2. Whether you are using a JDK or other development tool, all warning options are turned on when compiling, so compile

Can discover the platform-related statements as much as possible and give a warning. Although not guaranteed that there is no compile time warning

The wrong program must be a cross-platform, but the program containing warnings is very likely to be non-platform.

3. When using any method in the program, you must see the document in detail, make sure the method you use is not in the text.

A method that has been declared in the file (deprecated method), nor is it an implicit method that is not indicated in the document (

Undocumented method.

4. Try not to use java.lang.system's exit method when you exit Java programs. Exit method can terminate J

VM, thus terminating the program, but if another Java program is running, using the exit method will make the program also

Close, this is obviously not what we want to see. In fact, you must quit the Java program, you can use Destory ()

Out of a separate running process. For multi-threaded programs, you must turn off each non-daemon thread. Only in the program

The exit method exits the program when you use the exit method.

5. Avoid using local methods and local codes, write as much as possible to yourself, to rewrite the party

law. If you must use the local method, you can write a server program to call the method, and then you will now

The program is used as a client program for the server, or considers the CORBA (Public Object Request Agent)

Structure.

6. There is a method similar to Winexec in Delphi in Java, an Exec method for java.lang.Runtime class

, As this method itself has a platform-independent, but the command and command parameters called the method are and the flat.

Attached, therefore, avoid use when writing a program, if you must call other programs, you must

Let users set themselves to set this command and its parameters. For example, in Windows, you can call the NotePad.exe program.

The VI program is called in Linux.

7. All information in programming is used to use the ASCII code character set because it is not all operating systems.

With the Unicode character set, this cannot be a big bad news for cross-platform Java Chinese software programs.

8. Do not hardly encodes any constants associated with the platform, such as line separators, file separators, path separators, and the like, such as file separators, in UNIX and MACs. "

/ ", In Windows is" / ", if you want to use these constants, you need to use the JDAVA.UTIL.PROPERTIES class.

GetProperty method, such as java.util.properties.getProperty ("File.seParetor")

Get file separators, getProperty ("line.separator") returns row separator, getProperty (""

Path.separator ") Returns the path separator.

9. Do not use the gethostname method for java.net.inetaddress classes when writing cross-platform network programs

Get the host name, because the host name format of different platforms is different, it is best to use getaddress to get format

The same IP address, in addition, all hostnames in the program have to change to IP addresses, such as

Www.263.net is subject to phase

The IP address.

10. Programs relating to file operations require attention: Do not hardly encode file paths in the program, the reason and 8

It is especially important, so it is proposed separately. Moreover, the characters and the largest of different platforms for file names

Different file name length requirements, use the general ASCII code character as the name of the file when writing your program.

And cannot be conflicted with the program already existing in the platform, otherwise it will cause conflicts.

11. If the program you write is a GUI program, you should not set the size and position of the components when using the AWT component.

This use Java layout manager (Layout Manager) to set up and manage the size and location of the visual component, no

It is possible to cause a layout confusion.

12. Due to different operating systems, different machines, systems support color and screen size and resolution

How to get these attributes? Use the java.awt.systemcolor class to get the colors, such as this class

InactiveCaption is the background color of the event title in the window border, and Menu is the background color of the menu. Make

GetScreenResolution with java.awt.toolkit can be displayed in "pixel per inch" unit

Resolution. GetScreensize of this class can get the screen size (inches), LoadSystemColors can list

Some system colors.

- These precautions are from references, some are their experiences that they have long-term written Java programs. I believe

Help for your programming design.

转载请注明原文地址:https://www.9cbs.com/read-19105.html

New Post(0)