Compiling and Application of jar files in Java

Source: Internet
Author: User

Compiling and Application of jar files in Java

Author: Zhang guiquan

I think Java enthusiasts, especially beginners, have had such experiences. They like Java programming language (this person may be different ), but sometimes I always think that the advantage of Java is her "Fatal" disadvantage. Everyone's belief is still "one-time compilation and running everywhere", but it is because a little Java has lost a lot and we have lost a lot. As you may know, Microsoft's technology Microsoft products are the most admired in the industry .. Net/C #/. Vb/VB.net is also the most popular language and the most popular software development language. These languages are too easy to learn and use. Unlike Java. When developing highly efficient components in software development, it is hard to imagine that Java EJB can be used for development. Because of the immature technology and sound API, it is difficult for programmers to start. Although there are excellent development tools such as JBuilder, it is sometimes difficult to start (because their system is too complex, it will be a little difficult for the average person, when you want to develop an application ). The Java system is complicated enough. Because the Java language does not support the api(the. EXE file must be related to the hardware of the system. This is not required by the Java language ). However, you do not need to be disappointed, but you can use JBuilder, IBM, and softwares to support Java-enabled tools for program development and design, or use Java. jar Technology for related operations, that is, generate "double-click to run. JAR file. (As far as I know, JBuilder cannot run on a machine with 256 memory. It can be seen that JBuilder is too memory-consuming ). Now let's get down to the truth. In short ,. Jar files are useful. Let's start to enter the topic.

1. Create a JAR File

You must compile your. Java file before creating the. jar file. Suppose our file directory is C:/Java/myjava/Hello. Java
Assume that the file content of Hello. Java is:

/*
*
* Hello. Java
*
*/

Public class Hello {

Public static void Mian (string [] ARGs ){

System. Out. println ("Hello, world! ");

}

}

When creating a. jar file, you must switch the path to a folder in the file directory where hello. Java is located (. Jar files must be created in DOS (Windows platform), that is, myjava
Run the following command to include all the content in the current folder in the. jar file.

C:/Java/myjava> jar CF hello. jar.

Description: In the command line, jar is the call interface of the program that generates the. jar file. Cf is the hello. jar is the name given to the file. The last vertex (.) indicates that the tool will put all the files in the current directory into the jar file. The jar tool will perform the local operation when the folder contains more than one file.

2. view the JAR File
In fact, Jar files are compressed in WinZip format, so you can use WinZip to decompress the files. You can also use the following command line to view the files:

C:/Java/myjava> jar TF hello. Jar
You will see the JVM version in the local host and its producer Information
There are other information such as main-class: Hello. Hello.

3. Move the file to your favorite directory
If 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
Note: The JAR file can be closed, which means that each class in the specified package must be included in the same directory.

Iv. content list manifest. fm
The manifest. FM file is composed of key-value pairs. This includes the JVM version number or the vendor used to create the JVM.

5. Add the list content
If we edit the following addcontents.txt file

Name: myjava/Hello/
Sealed: True

You can use the following command to add it to our configuration file:

C:/Java/myjava> jar Umf addcontents.txt hello. Jar

6. Run the JAR File

You can run the JAR file in the following ways:
C:/Java/myjava> JAVA-classpath.-jar hello. Jar

Note: The-classpath. Part of the command is because the class PATH environment variable does not contain the (.) path.

Yes.
C:/Java/myjava/Hello> JAVA-jar hello. Jar

If your computer is in Windows XP, you can double-click the hello. jar file under your C:/Java/myjava/Hello, but you cannot see the running result.
(The graphic interface program can run like this). On Windows 2000, the JAR file is regarded as a. ZIP file, so you double-click it to decompress your file instead of running the file. Therefore, you only need to use the command line method.

After running the command, you will see
Hello, world!
.


Conclusion:

Here, the introduction of JAR File Creation and running is complete. You can verify it. Then create complex programs, such as advanced programs with interfaces.


If you are very interested in the jar file or want to have a deeper understanding of it, you can try to check more help information and many options explanations in the jar file under DOS.

Jar Help Group document content:

C:/> jar

Usage: jar {ctxu} [vfm0mi] [Jar-file] [manifest-file] [-C Directory] File Name...
Option:
-C. Create a new archive
-T list the archived content
-X expand the named (or all) files in the archive
-U: update an existing archive
-V generates detailed output to the standard output
-F specifies the archive file name
-M contains the marker information from the specified file.
-0 storage only; zip compression format not used
-M does not generate a manifest file for all items
-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 is recursively processed.
Both the configuration (manifest) file name and the archive file name must be specified, in the same order specified by the 'M' and 'F' signs.
Example 1: Archive two class files to an archive file named 'classes. jar:
Jar CVF classes. Jar Foo. Class bar. Class
Example 2: use an existing manifest file 'mymanifest'
Archive the file to an archive file named 'classes. jar:
Jar cvfm classes. Jar mymanifest-C Foo /.

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

This is an article I posted not long ago on the www.kissjava.com Forum. Today, I will drag it for you to enjoy!

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.