Docsity

Creating Jar Files: A Comprehensive Guide, Slides of Java Programming

Partial preview of the text

Download Creating Jar Files: A Comprehensive Guide and more Slides Java Programming in PDF only on Docsity!

Creating Jar Files

What is a Jar File?

  • Java archive (jar) files are compressed files that can store one or many files.
  • Jar files normally contain java or class files, but other files may also be included.
  • Jar files can be run on Windows by double clicking jar files if the JVM is installed.

Flow Chart

  • Create a manifest.mf file
  • Create a jar file
  • Basic jar commands
  • JAR subdirectories
  • Execute the jar file
  • Common mistakes
  • Bibliography

How to create a jar file:

Simple Method

  • The jar command a utility that comes with the JDK.
  • The format of the basic format of the jar command is: Jar cf jar-file input-files

How to create a jar file

  • The execution of this command creates the file: - Foo.jar

Executing Jar Application

  • Jar applications can be run with the following basic command: - java -jar jar-file
  • So to run our Foo.jar if it was an application use: - java -jar Foo.jar

Executing Jar Applets

  • Applets are invoked from applet tags in HTML code.
  • To execute an Applet simply add the name of the jar file into the applet tag is under the archive parameter.

Executing Jar Applets

  • For example, using the applet tag for your Foo.jar archive, you would use the following: <applet code=TicTacToe.class archive=”Foo.jar" width=120 height=120>

Create manifest.mf

  • Create a new notepad file named manifest.mf. This file contains a signal line that points out the main class. Here is the signal line. Main-Class:name of main class

 Blank line must be included because some Windows OS

versions need it. For example: myClass is my main Class. So manifest.mf is following. Main-Class: myClass

Jar file and Jar Commands

  • Next, create a jar file using the “jar” command in java. Here is the command. jar cvmf .jar manifest.mf A.class B.class …….. There are spaces between each file and class name.

 Here are the basic jar commands

Create Jar file => jar cf jar-file input-file View Jar file => jar tf jar-file Extract Jar file => jar xf jar-file

JAR Subdirectories

JAR Subdirectories

  • If we want to keep the same structure, we can compress files by typing: jar cmf Sample.mf Sample.jar Sample.class Turtle.class Sample.java Turtle.java images

Executing the Jar File

  • There are two ways to execute jar files.
    1. If an OS can read *.jar as javaw.exe or java.exe, users can double click jar files on the window. Otherwise:
    1. In a command window, type in

java –jar jar-file

Common Mistakes

  • Forgetting to leave a blank line after the main class declaration in the manifest.mf. OS may return “Fail to load Main-Class manifest attribute” error in Windows machines.
  • Failing to compress all your files. You have to type in all the files you want to compress. Separate these files with a space.