Precautions for writing cross-platform Java programs

zhaozj2021-02-16  155

The biggest advantage of using the Java language to write applications is "Run everywhere", this is not to say that all Java programs have cross-platform features. In fact, a considerable part of Java programs cannot be on other operating systems. Correctly run, so how can you write a real cross-platform Java program? Below is some things you need to pay attention to writing cross-platform Java programs: ---- 1. When writing Java cross-platform applications, you can choose JDK1.0, 1.1, 1.2 or support their GUI development tools such as JBuilder, Visual Age for Java, etc., but must pay attention to your Java program only use Java core API package. If you want to use a third-party class library package, the library package will also be completed by the Java core package, otherwise you have to release the JVM of the Java class library package when you release your program. That is, 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 irrelevant. ---- 2. Whether you are using a JDK or other development tool, all warning options are opened when compiling, so that the compiler can discover the platform-related statements as much as possible and give a warning. While not guarantees that the warning error is not compiled, it must be a cross-platform, but the program containing a warning error is very likely to be non-platform. ---- 3. When using any method in the program, you must see the document in detail, make sure that the method you use is not a way to expand Method in the document, nor the Idocunted Method, which is not indicated in the document. ---- 4. Try not to use java.lang.system's exit method when you exit Java programs. The exit method can terminate the JVM, thus terminating the program, but if another Java program is running, using the exit method will also turn the program close, which is obviously not what we want to see. In fact to exit Java programs, you can use Destory () to exit a separate running process. For multi-threaded programs, you must turn off each non-daemon thread. The exit method exits the program only when the program is absent from normal exit. ---- 5. Avoid using local methods and local codes, and write the Java class with the corresponding function as much as possible to rewrite the method. If you must use the local method, you can write a server program to call the method, then the program you want to write as the client program of the server, or consider the CORBA (Public Object Request Agent) program structure. ---- 6. There is a method similar to the WINEXEC in Delphi, the exec method of the java.lang.Runtime class, as the method itself is unrelated, but the command and command parameters called by the method are related to the platform. Therefore, when writing a program, avoid use, if you must call other programs, you must let the user to set the command and its parameters. For example, you can call the NotePad.exe program in Windows, and the VI program is called in Linux. ---- 7. All information in programming is used to use the ASCII code character set, because all operating systems support Unicode character set, which cannot be a big news for cross-platform Java Chinese software programs.

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

New Post(0)