Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the considerations for writing cross-platform Java programs

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the preparation of cross-platform Java procedures what points for attention, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to know about it.

1. When writing Java cross-platform applications, you can choose JDK1.0,1.1,1.2 or the GUI development tools that support them, such as Jbuilder,Visual Age for Java, etc., but 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 should also be developed by the Java core package. Otherwise, you will have to release the JVM that supports the Java class library package when you release your program. In other words, your program needs to be 100% pure Java. For example, Visual jacks + is not pure Java, and programs written by Visual jacks + are not platform independent.

2. Whether you are using JDK or other development tools, turn on all warning options at compile time so that the compiler can find as many platform-related statements as possible and give warnings. While there is no guarantee that programs that do not have compile-time warning errors are cross-platform, programs that contain warning errors are likely to be non-platform-independent.

3. When using any method in the program, look at the document carefully to make sure that the method you use is not an outdated method (Deprecated method) declared in the document, nor is it an implicit method (Undocumented method) that is not indicated in the document.

4. Try not to use java.lang.System 's exit method when exiting Java programs. The Exit method can terminate the JVM, thus terminating the program, but if another Java program is running at the same time, using the exit method will shut it down, which is obviously not what we want to see. In fact, to exit the Java program, you can use destory () to exit a stand-alone process. For multithreaded programs, each non-daemon thread must be closed. Use the exit method to exit the program only if the program exits abnormally.

5. Avoid using local methods and local code, write your own Java classes with corresponding functions as much as possible, and rewrite the method. If you must use this local method, you can write a server program to call the method, and then use the program you are writing now as the client of the server program, or consider the CORBA (Common object request Agent) 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 commands and command parameters that are called to the method are platform-related, so you should avoid using them when writing programs. 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. All the information in the program design should use the Unicode character set, because not all operating systems support the ASCII character set, which is bad news for cross-platform Java Chinese software programs.

8. Do not hard-code any platform-related constants in your program, such as line delimiters, file delimiters, path delimiters, etc. These constants are different on different platforms, such as file delimiters, which are "/" in UNIX and MAC and "" in windows. If you want to use these constants, you need to use the getProperty method of the jdava.util.Properties class. For example, java.util.Properties.getProperty ("file.separator") can get the file delimiter, getProperty ("line.separator") returns the line separator, and getProperty ("path.separator") returns the path separator.

9. When writing cross-platform network programs, do not use the getHostName method of the java.net.InetAddress class to get the hostname, because the hostname format of different platforms is different, it is best to use getAddress to get the same format IP address, in addition, all hostnames in the program should be changed to IP address, such as www.263.net should be changed to the corresponding IP address.

10. Programs involving file operations need to be careful: do not hard-code file paths in the program for the same reason as in 8, but this is particularly important, so it is proposed separately. Moreover, different platforms have different requirements for the characters used in the file name and the maximum length of the file name. When writing your program, you should use the general ASCII code character as the file name, and it cannot have the same name as the program that already exists in the platform, otherwise it will cause conflicts.

11. If the program you are writing is a GUI program, you cannot rigidly set the size and location of components when using AWT components. Instead, you should use Java's layout manager (layout manager) to set and manage the size and location of visual components, otherwise it may cause layout confusion.

12. Due to different operating systems, different machines, different colors and screen sizes and resolutions, how to get these properties? Use the java.awt.Systemcolor class to get the color you want, for example, inactiveCaption is the background color of the active title in the window border, and menu is the background color of the menu. Using java.awt.Toolkit 's getScreenResolution, you can display the resolution of the screen in pixels per inch. The getScreenSize of this class can get the screen size (inches), and loadSystemColors can list all the system colors.

Thank you for reading this article carefully. I hope the article "what are the points for attention in writing cross-platform Java programs" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report