Java

 

 

 

 

Java - jar file  

 

 

 

Packing a whole package folder into a jar file

 

In this example, I will pack a whole package folder created in Package tutorial. The directory structure and the files in each of the folders are as shown below.

 

    c:\temp>dir/s

     

     Volume in drive C has no label.

     Volume Serial Number is 568E-ECD3

     

    Directory of c:\temp

     

    01/05/2018  10:53 PM               144 Hello.java

    01/06/2018  01:33 AM               161 HelloWorld.java

    01/06/2018  02:45 AM    <DIR>          MessageCollections

    01/06/2018  01:35 AM               389 PrintMessage.java

    01/05/2018  10:53 PM               144 World.java

     

     

    Directory of c:\temp\MessageCollections

     

    01/06/2018  02:45 AM    <DIR>          .

    01/06/2018  02:45 AM    <DIR>          ..

    01/06/2018  02:45 AM    <DIR>          Hello

    01/06/2018  02:43 AM    <DIR>          HelloWorld

    01/06/2018  02:45 AM               515 PrintMessage.class

    01/06/2018  02:45 AM    <DIR>          World

                   1 File(s)            515 bytes

     

     

    Directory of c:\temp\MessageCollections\Hello

     

    01/06/2018  02:45 AM               416 Hello.class

     

     

    Directory of c:\temp\MessageCollections\HelloWorld

     

    01/06/2018  02:43 AM               438 HelloWorld.class

                   

     

     

    Directory of c:\temp\MessageCollections\World

     

    01/06/2018  02:45 AM               416 World.class

                   

     

 

You can pack every files in the folder and the sub folders using following command.

    c:\temp>jar cvf MessageCollections.jar .

    // you will see the print out when you execut this command

     

    added manifest

    adding: Hello.java(in = 144) (out= 110)(deflated 23%)

    adding: HelloWorld.java(in = 161) (out= 119)(deflated 26%)

    adding: MessageCollections/(in = 0) (out= 0)(stored 0%)

    adding: MessageCollections/Hello/(in = 0) (out= 0)(stored 0%)

    adding: MessageCollections/Hello/Hello.class(in = 416) (out= 291)(deflated 30%)

    adding: MessageCollections/HelloWorld/(in = 0) (out= 0)(stored 0%)

    adding: MessageCollections/HelloWorld/HelloWorld.class(in = 438) (out= 298)(deflated 31%)

    adding: MessageCollections/PrintMessage.class(in = 515) (out= 323)(deflated 37%)

    adding: MessageCollections/World/(in = 0) (out= 0)(stored 0%)

    adding: MessageCollections/World/World.class(in = 416) (out= 290)(deflated 30%)

    adding: PrintMessage.java(in = 389) (out= 175)(deflated 55%)

    adding: World.java(in = 144) (out= 110)(deflated 23%)

 

 

Then you will see a jar file has been created as shown below.

 

    c:\temp> dir

     

     Volume in drive C has no label.

     Volume Serial Number is 568E-ECD3

     

     Directory of c:\temp

     

    01/06/2018  11:40 PM    <DIR>          .

    01/06/2018  11:40 PM    <DIR>          ..

    01/05/2018  10:53 PM               144 Hello.java

    01/06/2018  01:33 AM               161 HelloWorld.java

    01/06/2018  02:45 AM    <DIR>          MessageCollections

    01/06/2018  11:40 PM             3,710 MessageCollections.jar

    01/06/2018  01:35 AM               389 PrintMessage.java

    01/05/2018  10:53 PM               144 World.java

 

 

 

Running a class within a jar file

 

    c:\temp>java -cp MessageCollections.jar MessageCollections.PrintMessage

     

    // MessageCollections.PrintMessage is the class file to run.