How to Use the jar command to package the Java Project

Source: Internet
Author: User

I. Jar command usage:
In the CMD command window, enter jar and press enter to display the command usage instructions:

Ii. Example (This section describes how to generate a double-click jar package that can be executed):

1. First, we will introduce how to execute the package under the command line.
1. The simplest project without a package structure.
(1) create a new file "helloworld. Java" under drive C ":

1 public class helloworld
2 {
3 Public static void main (string [] ARGs ){
4 system. Out. println ("Hello world! ");
5}
6} (2) Enter C:/javac helloworld. Java in the command line and compile "helloworld. Class" in the C: disk ".

(3) create a file "menefest" on the drive (without a suffix ):
Main-class: helloworld


Note that there must be a blank line at the end; otherwise, a class error cannot be found.

(4) package: Enter C:/jar cvmf menefest helloworld. Jar helloworld. Class and generate "helloworld. Jar" on the C: disk ".

(5) Run: Enter C:/Java-jar hellowolrd. jar. "Helloworld" is displayed on the screen ".

. A Java project with a package structure.
(1) create a new file "helloworld. Java" in C:/COM/gosyl/demo ":

Package com. gosyl. Demo;

Public class helloworld {
Public static void main (string [] ARGs ){
System. Out. println ("helloworld ");
}
}

(2) Enter C:/javac COM/gosyl/demo/helloworld in the command line. java: compile and generate "helloworld." In the C:/COM/gosyl/demo/folder. class ".

(3) create a file "menefest2" under drive C (no suffix ):

Main-class: COM. gosyl. Demo. helloworld


Note that there must be an empty line at the end, and a space is followed by the colon. Otherwise, a class error cannot be found.

(4) Packaging: Enter C:/jar cvmf menefest2 helloworld. Jar COM/and generate "helloworld. Jar" on the C: disk ".

(5) Run: Enter C:/Java-jar helloworld. jar. "Helloworld" is displayed on the screen ".

1. 3. reference the Java project of the external jar package.
(1) create a new file "car. Java" in C:/COM/gosyl/demo ":

Package com. gosyl. Demo;

Public class car
{
Public static void main (string [] ARGs ){
Light. On ();
}
}

(2) create a new file "light. Java" in C:/COM/gosyl/demo ":

Package com. gosyl. Demo;

Class light
{
Public static void on (){
System. Out. println ("light is on! ");
}
}

(3) Package 1: Enter C:/jar CVF light. Jar COM/gosyl/demo/light. Class and generate "light. Jar" on the C: disk ".

(4) create a file "menefest-car" under drive C (no suffix ):

Main-class: COM. gosyl. Demo. Car
Class-path: light. Jar

Note that there must be an empty line at the end, and a space is followed by the colon. Otherwise, a class error cannot be found.

(5) package 2: Enter C:/jar cvmf menefest-car. Jar COM/gosyl/demo/car. Class, and generate "car. Jar" under drive C ".

(6) Run: Enter C:/Java-jar car. jar. The screen displays "light is on ".

Iii. Summary
In the list file menifest, the format is very high. Note that each colon must be followed by a space, and the file must end with a blank line. For projects that reference external jar packages, you must define the class-path attribute in manifest.

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.