Authoring and application of jar files in JAVA

Source: Internet
Author: User
Tags format command line contains interface zip
Authoring and application of jar files in Java

I think the Java enthusiasts, especially beginners have had the experience, that is, like the Java programming language (this person may not be the same situation), but sometimes always feel that the advantages of Java is her "fatal" shortcomings. Everyone's belief is still "compile, run everywhere" but because to a little Java lost a lot, we also lost a lot. As we all know, the industry is now most admired by Microsoft's technology Microsoft's products. net/c#/. Vb/vb. NET is also the most popular language, the most popular software development language. Because these languages are too easy to learn and good to use. It's not as hard to understand as the Java language. When software development brings about efficient component development, it is difficult to imagine using Java EJB to develop, because immature technology and unsound API, make the programmer difficult to start. Although there are jbuilder such good development tools, but sometimes difficult to start (because their system is too complex, for the general people will be a bit difficult, when you want to develop applications). The Java system is complex enough. because "cross-platform = sex" The Java language, or JDK, does not support the corresponding APIs that produce the. exe file (should be generated). EXE files are always related to the hardware of the system, which is not required by the Java language. But there's no need to disappoint you, but you can use JBuilder or IBM and soft Java-enabled tools to develop and design programs, or Java. Jar technology to do the related operations, that is, to generate a "double-click can be run." Jar file, which is a good thing. (As far as I know JBuilder is not able to run on 256 memory machines, it is too memory-consuming). OK, now let's get down to the moment. Anyway. The jar file is still useful. Let's go to the subject.

First, make Jar file

Before making a. jar file, you must first compile your. java file. Let's say our file directory is C:\java\myJava\hello\Hello.java.
Now assume that the contents of the Hello.java file are:

/*
*
* Hello.java
*
*/


public class Hello {

public static void Mian (string[] args) {

System.out.println ("Hello, World!");

}

}

At the time of the. jar making you must switch the path to the Hello.java folder (. jar file to be created in DOS (Windows platform)), that is, the Myjava
Run the following command to include all content under the current folder in the. jar file.

C:\java\myjava>jar CF Hello.jar.

Description: The jar on the command line is the calling interface of the program that generated the. jar file. CF is a pair of options. The Hello.jar is the name we gave the file, and the last point (.) Instructs the tool to put all the files in the current directory into the jar file, and the Jar tool to do the work, when the folder contains more than one file.



Second, look at the jar file
In fact, the jar file is in WinZip format for file compression, so you can use WinZip to extract the file, you can also use the following command line to see:

C:\java\myjava>jar TF Hello.jar
You will see the version of the JVM in the local machine and the manufacturer's information.
There are main-class:hello.hello and other information.


Move the file to your favorite directory
Let's say you want to move the jar file to the directory: D:\Java2\myFirst, run the following command:
d:\java2\myfirst> Jar XF C:\java\Hello.jar
Description: The jar file can be closed, meaning that each class in the specified package must be included in the same directory.

IV: Content List manifest.fm
The Manifest.fm file is made up of key-value pairs. It contains the version number of the JVM mentioned earlier/the creation of his vendor, and so on.

V. Increasing the contents of the list
When we edit one of the following addContents.txt files

Name:myjava\hello\
Sealed:true

You can add it to our manifest file by using the following command:

C:\java\myjava>jar UMF AddContents.txt Hello.jar

Six, run the jar file

You can run the jar file in the following ways:
C:\java\myjava>java-classpath. -jar Hello.jar

Description: Need to command the-classpath. This is partly because there is no (.) path in the PATH environment variable for the class.


can also
C:\java\myjava\hello>java-jar Hello.jar

If your computer is a Windows XP version you can also go directly to your C:\java\myJava\Hello to double-click the Hello.jar file but you can't see the results of the run
(Graphical interface programs can be run like this), on Windows 2000, the jar file is treated as a. zip file so you double-click him to unzip your file instead of running the file. So you only have to take the command line.


After you run the command, you will see the display on your screen
Hello, World!
Words.


Conclusion:

Here we have the description of the creation and operation of jar files is over. You can try to verify it. Then create a complex program, such as an advanced program with an interface.


If you are interested in the jar file or want to have a deeper understanding of her then you can tap the jar in DOS to look for more help and many options to explain.


Jar Help Group document content:


C:\>jar

Usage: jar {ctxu}[vfm0mi] [jar-file] [manifest-file] [-c directory] filename ...
Options:
-C Create a new archive
-t lists list of archived content
-X expand named (or all) files in the archive
-U Update Existing archive
-V build verbose output to standard output
-F Specify archive file name
-M contains marked information from the indicated file
-0 storage mode only; not in zip compression format
-M does not produce a manifest for all items (manifest) file
-I generates index information for the specified jar file
-C changes to the specified directory and contains the following files:
If a file name is a directory, it will be recursively processed.
The list (manifest) filename and archive file name) is specified in the same order as the ' m ' and ' F ' flags.
Example 1: Archive two class files into an archive file called ' Classes.jar ':
Jar CVF Classes.jar Foo.class bar.class
Example 2: Using an existing manifest (manifest) file ' Mymanifest ' will foo/the directory of all
Files are archived to an archive file called ' Classes.jar ':
Jar CVFM Classes.jar mymanifest-c foo/.

......................................................................................................................... ............................



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.