How to run the jar

Source: Internet
Author: User

First, make Jar file

Before you can create a. jar file, you must first compile your. java file. Let's say our file directory is C:javamyjavahellohello.java.
Now suppose Hello.java's file content is:

/*
*
* Hello.java
*
*/


public class Hello {

public static void Mian (string[] args) {

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

}

}

During the. jar authoring you must switch the path to the outer folder of the file directory where the Hello.java resides (the. jar file to be created in DOS (Windows platform)), or Myjava
Run the following command to include all content under the current folder in the. jar file.

C:javamyjava>jar CF Hello.jar.

Description: The jar on the command line is the calling interface for the program that generated the. jar file. CF is a pair of optional (options) behind the Hello.jar is the name we give the file, the last dot (.) indicates that the tool puts all the files in the current directory into the jar file, and the Jar tool is returned to the folder when there are 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 files, you can also use the following command line to view:

C:javamyjava>jar TF Hello.jar
You will see the version of the JVM in the local machine and its manufacturer information
There are main-class:hello.hello and other information.


Third, move the file to the directory you like
Suppose you want to move the jar file to the directory: D:java2myfirst, run the following command:
d:java2myfirst> Jar XF C:javahello.jar
Note: A jar file can be closed, which means that each class in the specified package must be contained in the same directory.

Four: 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/created his vendor, etc.

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

Name:myjavahello
Sealed:true

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

C:javamyjava>jar UMF AddContents.txt Hello.jar

VI. Run the jar file

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

Description: Requires the-classpath of the command. This is partly because the PATH environment variable in the class does not have a (.) path.


can also
C:javamyjavahello>java-jar Hello.jar

If your computer is a Windows XP version, you can also double-click the Hello.jar file directly under your C:javamyjavahello but you can't see the results of the run
(The graphical interface program can run this way), on Windows 2000, the jar file is considered a. zip file so you can double-click on it to unzip your file instead of running the file. So you only have to use the command line way.


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


Conclusion:

The introduction of the creation and operation of our jar files has ended here. You can try to verify it. Then create complex programs, such as high-level programs with interfaces.


If you are interested in the jar file or want to know more about her then you can look at the DOS down-typing jar to see more help information and many options explanations.


Jar Help Group document content:


C:>jar

Usage: jar {ctxu}[vfm0mi] [jar-file] [manifest-file] [-C directory] File name ...
Options:
-C Create a new archive
-t lists the list of archived content
-X expands the named (or all) file in the archive
-U updates an existing archive
-V generate concrete output to standard output
-f Specifies the archive file name
-M contains marked information from the indicated file
-0 storage mode; Zip compression format not used
-M does not produce a list of 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 processed recursively.
The manifest (both the Manifest) file name and the archive file name need to be specified, in the same order specified by the ' m ' and ' F ' flags.
Example 1: Archive two class files into an archive file named ' Classes.jar ':
Jar CVF Classes.jar Foo.class bar.class
Example 2: Use an existing manifest (manifest) file ' Mymanifest ' to all the foo/directories
The file is archived to an archive file named ' Classes.jar ':
Jar CVFM Classes.jar mymanifest-c foo/.
This post from Zdnetchina Chinese community http://bbs.zdnet.com.cn, this address: http://bbs.zdnet.com.cn/viewthread.php?tid=584158

How to run the jar

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.