Presentation is loading. Please wait.

Presentation is loading. Please wait.

Executable Jars Dan Fleck Fall 2007 12/27/2018.

Similar presentations


Presentation on theme: "Executable Jars Dan Fleck Fall 2007 12/27/2018."— Presentation transcript:

1 Executable Jars Dan Fleck Fall 2007 12/27/2018

2 Introduction Executable Jars allow a program to be packaged and run more simply Today’s Agenda How to run Java Programs How to Create Executable Jars 12/27/2018

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\ 12/27/2018

4 How to Run Java Programs
C:\javacode\myproject\cls\ edu How to Run Java Programs gmu Run using the classes cd to directory C:\javacode\myprojects\cls java edu.gmu.pack1.MyClass Run using the classpath java -cp c:/javacode/myproject/cls edu.gmu.pack1.MyClass Run using the the Jar java -cp myjar.jar edu.gmu.pack1.MyClass Run using an executable Jar java -jar myexecutablejar.jar pack1 pack2 images 12/27/2018 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 it jar cvf myjar.jar * c = create new archive v = verbose output f = archive file name Check it! jar tvf myjar.jar t = display table of contents pack2 images 12/27/2018

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 12/27/2018

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! 12/27/2018

8 Creating the executable jar
C:\javacode\myproject\cls\ edu Creating the executable jar 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 option: Jar cvfm myjar.jar mymanifest * m = use specified manifest pack2 images 12/27/2018

9 Creating the executable jar
C:\javacode\myproject\cls\ edu Creating the executable jar gmu pack1 Test it: java -jar myjar.jar pack2 images 12/27/2018

10 Advantages Jars make code more portable by keeping it all together
Executable jars make the code easier to run Note: 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. 12/27/2018


Download ppt "Executable Jars Dan Fleck Fall 2007 12/27/2018."

Similar presentations


Ads by Google