Executable Jars - PowerPoint PPT Presentation

About This Presentation
Title:

Executable Jars

Description:

Executable Jars allow a program to be packaged and run more simply ... How to Create a Jar. Try running it! java -cp myjar.jar edu.gmu.pack1.MyClass. edu. gmu ... – PowerPoint PPT presentation

Number of Views:172
Avg rating:3.0/5.0
Slides: 11
Provided by: csG6
Learn more at: https://cs.gmu.edu
Category:
Tags: ajar | executable | jars

less

Transcript and Presenter's Notes

Title: Executable Jars


1
Executable Jars
  • Dan Fleck
  • Fall 2007

2
Introduction
  • Executable Jars allow a program to be packaged
    and run more simply
  • Todays Agenda
  • How to run Java Programs
  • How to Create Executable Jars

3
How 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\
4
How 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!)
5
How 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
6
How to Create a Jar
C\javacode\myproject\cls\
edu
gmu
pack1
  • Try running it!java -cp myjar.jar
    edu.gmu.pack1.MyClass

pack2
images
7
Question
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!
8
Creating 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
9
Creating the executable jar
C\javacode\myproject\cls\
edu
gmu
pack1
  • Test it
  • java -jar myjar.jar

pack2
images
10
Advantages
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com