Title: Executable Jars
1Executable Jars
2Introduction
- Executable Jars allow a program to be packaged
and run more simply - Todays Agenda
- How to run Java Programs
- How to Create Executable Jars
3How to Run Java Programs
- Assume we are in directory
- C\javacode\myprojects\cls
- Contents of that directory are
MyClass.class Shape.class Ball.class
edu\
Utils.class Elephant.class
gmu\
Ball.jpg Elephant.jpg Sphere.gif
pack1\
pack2\
images\
4How to Run Java Programs
C\javacode\myproject\cls\
edu
gmu
- Run using the classes cd to directoryC\javacode
\myprojects\clsjava edu.gmu.pack1.MyClass - Run using the classpathjava -cp
c/javacode/myproject/cls edu.gmu.pack1.MyCl
ass - Run using the the Jarjava -cp myjar.jar
edu.gmu.pack1.MyClass - Run using an executable Jarjava -jar
myexecutablejar.jar
pack1
pack2
images
Executable Jars are the easiest! (Less thinking!)
5How to Create a Jar
C\javacode\myproject\cls\
edu
gmu
pack1
- cd to the top level directory of your package.
(c\javacode\myproject\cls) - Create itjar cvf myjar.jar c create new
archivev verbose outputf archive file name - Check it!jar tvf myjar.jart display table of
contents
pack2
images
6How to Create a Jar
C\javacode\myproject\cls\
edu
gmu
pack1
- Try running it!java -cp myjar.jar
edu.gmu.pack1.MyClass
pack2
images
7Question
C\javacode\myproject\cls\
edu
gmu
pack1
- Question What did java need to know in order to
run the program? - java -cp myjar.jar edu.gmu.pack1.MyClass
- Answer Which class is the main-class!java -cp
myjar.jar edu.gmu.pack1.MyClass
pack2
images
An executable Jar just needs to know the
main-class in order to run!
8Creating the executable jar
C\javacode\myproject\cls\
edu
gmu
pack1
- cd to the top level directory of your package.
(c\javacode\myproject\cls) - Create a manifest text file that holds the
main-class Notepad mymanifest Manifest-Version
1.0 Main-Class edu.gmu.pack1.MyClass - Create the jar with -m optionJar cvfm myjar.jar
mymanifest m use specified manifest
pack2
images
9Creating the executable jar
C\javacode\myproject\cls\
edu
gmu
pack1
- Test it
- java -jar myjar.jar
-
pack2
images
10Advantages
- Jars make code more portable by keeping it all
together - Executable jars make the code easier to
runNote On some systems (Windows, some Unix
variants) Jar files are associated with the java
-jar command, so simply double-clicking on an
executable Jar will run it.