Package program is a JAR executable

xiaoxiao2021-03-06  95

Source: http://dev.9cbs.net/Article/39/39046.shtm: Not all types of Java applications can be packaged in a separate JAR file. Must meet certain conditions, a Java application All files can be packaged into a file. In addition, there are two kinds of JAR files, one is a manifest file, one is there is no list file (this main purpose to store the Java library file). List There are many functions, I only discuss this list file that can use Java to run JAR files. Just use the command java -jar myjarfile.jar to run JAR files directly. This JAR file list is as follows : Manifest-Version: 1.0main-class: mainclassFilecreated-by: Your Company. Have the main program inside your program is com.abc.maInframe, then your list of this list is: Manifest-Version: 1.0mAin -Class: com.abc.mainframecreated-by: ABC Company. Then I would like to discuss the problem of writing to the application of a separate JAR file requires attention. Program resource files (such as * .gif, * .jpg, *. JPEG, * .properties, etc. You cannot use physical paths, you can only use the relative resource path relative to the current class file, only this can guarantee that the package will find these resource files. So, these files are used when opening. The following statement: URL URL = getClass (). Getresource (String name); or InputStream IN = getClass (). GetResourceAsSstream (String name) ;., javax.swing.imageicon icon = new javax.swing.imageicon ("1. GIF "); then the packaged program will not find this picture because it references a physical path picture, it should be changed to this: javax.swing.Imageicon icon = new javax.swing.imageicon (getClass (). getreso Urce ("1.gif")); other resource files are similar to this. However, the use of the property file (* .properties), Java automatically loads the properties file in the JAR file, for example: ResourceBundle.getBundle "com.abc.test"); then, as long as this file is placed in the JAR file directory structure, Java can automatically find this file. I wanted to discuss how to create JAR using JAR tools. File, this tool can be found under / bin, the file name is JAR.exe in the Win32 version, UNIX is JAR. Enter the jar command under the command line window, will see the following output: Jar {CTXu } [VFM0M] [JAR- File] [Manifest- File] [-c Directory] File Name ... Option: -c Create a new archive -T list list -X expands named in the archive (or All file -U updates existing archive -V generation detailed output to standard output -f specified archive file name -M contains a list of specified lists (Manifest] information -0 storage mode; Use ZIP compressed format -M does not produce a list of all items (

The manifest] file -i generates index information -c to the specified JAR file to change to the specified directory and contains the following file: if a file name is a directory, it will be processed. The list (manifest) file name and archive file name need to be specified, press the same reference to the 'm' and 'f' flag 1: Archite the two class files into an archive file named 'classes.jar': JAR CVF CLASS.JAR FOO.CLASS BAR.CLASS Example 2: Use a list of existing list (Manifest) file 'mymanifest' archived all files in the foo / directory into an archive file named 'Classes.jar': JAR CVFM classes.jar mymanifest -c foo /. Suppose you want to pack the above COM.ABC.MAINFRAME to package an application of the main program as a separate JAR file, then build content in the root directory of the program as described above The list file, the file name is manifest.mf, then under the command line window, the current directory is the program root directory, enter the following command: jar cvfm abcapp.jar manifest.mf *. *, Then you will use an existing list The file creates a file called abcapp.jar and adds all files (including subdirectory) in this JAR file. If you want to use the non-JAR tool to establish a JAR file, it is recommended to use WinRAR. First, you still need to create a list. File (the name must be manifest.mf), write the same content as the JAR tool is written to this file, then you need to create a Meta-INF subdirectory, then put the manifest.mf in this directory, then keep the directory Structure All of this content uses WinRAR compresses files to zip format, the file name is taken as abcapp.jar. As shown below, the directory structure is: | --- / meta-inf / manifest.mf | - --- COM / ABC / MainFrame.class. Select the files in each subdirectory and root directory in the program root directory, click the right mouse button, select "Add to Compressed Pack ..." command, and then specify the file name as ABCAPP. JAR, format is ZIP, and finally click "OK" to complete. Author Blog:

http://blog.9cbs.net/robertb9527/

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

New Post(0)