How to Create JAR Files: 10 Steps (with Images)

Table of contents:

How to Create JAR Files: 10 Steps (with Images)
How to Create JAR Files: 10 Steps (with Images)
Anonim

The. JAR file format is a compressed format used primarily for distributing Java applications and libraries. It is derived from the. ZIP file format and works in a very similar way. The data files are compressed into a single archive, making it easier to distribute them across a network. If you need to package a Java application, or a collection of classes, you can do so by creating a JAR file, using the Java Development Kit (JDK) and command prompt.

Steps

Method 1 of 2: Windows

Create JAR File Step 1
Create JAR File Step 1

Step 1. Prepare the files

Create a single folder, and move all the files you want to insert in the JAR archive into it. This step is mandatory, because creating the JAR file, via a single command, cannot specify more than one path to retrieve the files.

Create JAR File Step 2
Create JAR File Step 2

Step 2. Open Command Prompt

From the 'Start' menu select 'Run' and, in the 'Open' field, type 'cmd'. At this point click 'OK'.

Create JAR File Step 3
Create JAR File Step 3

Step 3. From the command prompt, navigate to the folder containing all the files to be placed in the JAR archive

Normally, the starting path of the command prompt should be 'C: \>'.

  • To move to a directory use the command 'cd' ('change directory'), typing 'cd. Conversely, to move to the previous directory, type the command 'cd'.
  • For example, if your files are located in the 'C: / myfiles' folder, you will need to use the 'cd / myfiles' command.
  • To go directly to the folder, hold down the shift key, right click on the folder in Windows Explorer, then select the command "Open window here".
Create JAR File Step 4
Create JAR File Step 4

Step 4. Set the path to access the JDK 'bin' directory

To create a JAR file, you will need to use the 'jar.exe' command, which resides right in this location.

  • Use the 'path' command, to set the path relative to the 'bin' directory of the JDK. For example, having installed the JDK in the default location, you will need to type the following command: 'path c: / Program Files / Java / jdk1.5.0_09 / bin'.
  • If you are not sure if the path is correct, use Windows 'Explorer' to navigate to the correct folder, then make a note of the entire path you will find in the address bar.
Create JAR File Step 5
Create JAR File Step 5

Step 5. Create the JAR file

The syntax of the command to create the JAR archive will be as follows: 'jar cf'.

  • 'jar' is the command to invoke the 'jar.exe' program which compiles and creates the JAR files.
  • The 'c' parameter specifies that you are creating a JAR file.
  • The 'f' parameter indicates that you want to specify the name of the JAR file.
  • The 'jar-file-name' parameter is the name under which the JAR archive will be created.
  • The 'filename or list of files' parameter is the list of files, separated by a space, that will be included in your JAR file
  • For example, you might have a command like this 'jar cf myfilejar manifest.txt myclass.class'. This command will create a JAR file called 'myfilejar.jar', which will include within it the 'manifest.txt' and 'myclass.class' files.
  • If you specify a directory name in the file list, the jar.exe command will automatically include all its contents in your.jar file.

Method 2 of 2: Mac

Create JAR File Step 6
Create JAR File Step 6

Step 1. Prepare the files

Create a single folder and move all the files you want to insert into the JAR archive into it.

Open a 'Terminal' window. Navigate to the directory, where you will find all the files to include in your JAR archive

Create JAR File Step 7
Create JAR File Step 7

Step 2. Compile all the.java class files

For example, compile the HelloWorld.java file using the following command:

  • javac HelloWorld.java
  • The result of the above command is a file with the extension '.class', which you can include in your JAR file.
Create JAR File Step 8
Create JAR File Step 8

Step 3. Create your manifest file

Using a text editor, create your manifest file, save it with the extension '.txt' and insert the following text in it:

Main-Class: HelloWorld (replace HelloWorld with the name of your.class file)

Create JAR File Step 9
Create JAR File Step 9

Step 4. Create your JAR file using the following command:

jar cfm HelloWorld.jar Manifest.txt HelloWorld.class

Create JAR File Step 10
Create JAR File Step 10

Step 5. Run the "java -cp filename.jar maiclass" file

Advice

  • You can also create JAR files using the programs for compressing archives, the same ones with which ZIP files are created. If you use this method, make sure the manifest file is the first one you include in the archive.
  • JAR files can be digitally signed to increase security. You can do this very easily by using the JDK's 'jarsigner' command.

Recommended: