Precautions for writing cross-platform Java programs

xiaoxiao2021-03-06  48

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.

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, 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 file separator GetProperty ("line.separator") Returns the line separator, getProperty ("path.separator") returns the path separator. 9. When writing cross-platform network programs, don't get the host name, because the host name format of different platforms is different, it is best to get the same IP address in the format using GetDress, and the program All hostnames are replaced by IP addresses, such as www.263.net, to replace the corresponding IP address.

10. Programs relating to file operations require attention: Do not use hard coded file paths in the program, just as in 8, just this is especially important, so separately. Moreover, different platforms are different from the requirements of the characters and maximum file names used by the file name. When writing your program, use the general ASCII code character as the name of the file, and cannot be with the program already existing in the platform, otherwise it will Conflict.

11. If the program you write is a GUI program, you should use the size and location of the components when using the AWT component, and you should use the Java Layout Manager to set up the size and location of the visual components, otherwise it is possible to cause The layout is chaotic.

12. How to get these attributes due to different operating systems, different machines, systems supported by different machines, and the size and resolution of the screen, how to get these attributes? Using the Java.awt.SystemColor class to get the colors, such as this class is the background color of the active title in the window border, Menu is the background color of the menu. GetScreenResolution using java.awt.toolkit can display the resolution of the screen in "pixel per inch". The getScreensize of this class can get the screen size (inches), and LoadSystemColors can list all system colors.

- These precautions are some of the references, some are their own experiences of the Java program for a long time, and believe that it will help your programming. Attachment: Get systematically attribute in Java, with its object-oriented, cross-platform, good portability, high security, and is favored by numerous programming people, more and more people use their first choice for application software development. . At the Java application run, especially when running in a cross-platform work environment, you need to determine how the operating system type, user JDK version, and user working directory, etc., to ensure that the program is operating correctly. In general, work environment information can be quickly obtained using methods in the system attribute class (Properties) provided by JDK. In addition, program developers can also define system properties files related to the application, dynamically load programmers defined attribute files during user program execution to control program operation. This article describes how to define system properties files in connection with the actual examples of the system attribute class, and discuss the definition of the security policy file. Inheritance attribute class Java Properties class as follows: java.lang.Object  - java.util.Dictionary  - java.util.Hashtable  - java.util. Properties When the application starts execution, the program first reads the default properties of the system. If the user property file is defined, the application loads the property file. The program run can dynamically modify the attribute definition according to the execution, and save the properties file before the program ends. Get attributes: ● Contains (Object value): If a given parameter or attribute key is defined in the property table, the method returns true, otherwise returns false; ● getProperty (String Key), GetProperty (String Key, String Default): Get keyword values ​​based on a given attribute keyword; ● List (PrintStream S), List (PrintWriter W): Output attribute table content in the output stream; ● Size (): Return the current The number of properties defined in the property table. Method for setting up properties: ● PUT (Object Key, Object Value): Additional property keywords and keywords to the property table; ● Remove (Object Key): Delete keywords from the property table. Get System Properties System Properties refers to operating system configuration information and software information related to user programs. The property keywords that are usually related to the user program include: ● File.Separator: file separator, "/" in the Windows environment, "/" in the UNIX environment; ● User.home: User main directory; ● java.home: Java's real-time installation directory; ● Java.ext.dirs: JAVA.EXT.DIRS: JDK installation directory; ● Os.name: Operating system name; ● user.name: User login name; ● Os.Version: Operating system version; ● PATH. Separator: The path separator of the current operating system; ● User.dir: The current user program is located. Next, a method of obtaining a system attribute will be described below.

/*getSystemProperties.java*/ import java.util.Properties; public class getSystemProperties {public static void main (String args []) {// attributes system configured by obtaining attribute class prop Properties prop = new Properties (System.getProperties () ); // Output system properties in the standard output Prop.List (System.out);} // Depending on the acquired system attribute determines the program execution process ...} After the above program is executed, it produces a similarity similar to the Windows environment. Output: user.home = c: / win95 java.home = d: /jdk1.2/jre java.ext.dirs = d: /jdk1.2 Os.name = Windows 95 user.name = office java.vm.name = Classic VM OS.Version = 4.10 path.seParetor =; file.sePare = / user.dir = d: / javatest Attribute file Operation method Java program developer can set the parameters running by defining attribute files. The property file is a program external file. When the application is initially run, you can get the parameters of the program run by reading the properties file. For example, the execution process of the program external control program is required at runtime, which can be achieved by defining the method of defining the attribute file.

Next, the example of the operating method of the attribute file is explained: /*PropertyFile.java*/ // Introduced related class import java.io. *; import java.util.properties; public class print {// definition file input and output stream static fileInputstream FIS; Static FileOutputStream Fos; Public Static Void Main (String Args []) {// Generate New Properties Object Properties Prop = New Properties (); try {// Generate file input and output flow, input stream points to users have defined Attribute file, output stream points fis = new fileinputstream ("firstprop.txt"); fos = new fileoutputstream ("secondprop.txt");} catch (filenotfoundexcection); ("Cannot Create THE File Stream");} Try {// From Enter File Load System Properties Prop.Load (FIS); // Change the value of attribute keywords in Prop.Put ("Switch", "according to the program execution "); // Output new property file secondprop.txt prop .save (fos," - a new profment ");} catch (ioException e) {system.out.println (" Exception in repleace the keyword ") Before the program execution, the user must first define the attribute file firstprop.txt, which is as follows: switch = 0 version = 1.0 Directory = JavateSt program runs, output a new property file secondprop.txt, the file content is as follows (Note the difference between the two file key Switch contents): # - a new profrity file - #sun Mar 11 21:22:40 cst 2001 switch = 1 Version = 1.0 Directory = JavaTest can be seen from the previous example, and the attribute class Properties provided through JDK can easily control the execution process of the application outside the program to simplify the programming difficulty, making the program process more controllability. The security policy of the application environment provided by the security policy file Java enables different code to have different access licenses for system resources. The Java application security policy is expressed by the Policy object and is implemented by defining security policy files. Java 1.2 security policy files are divided into three levels: system security policy files, user security policy files, and default security policy files. When the Java application is started, load the security policy content in order.

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

New Post(0)