SlideShare a Scribd company logo
1
CSE 331
Java Packages; JAR Archives
slides created by Marty Stepp
based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
http://www.cs.washington.edu/331/
2
Java packages
• package: A collection of related classes.
 Can also "contain" sub-packages.
 Sub-packages can have similar names,
but are not actually contained inside.
•java.awt does not contain java.awt.event
• Uses of Java packages:
 group related classes together
 as a namespace to avoid name collisions
 provide a layer of access / protection
 keep pieces of a project down to a manageable size
3
Packages and directories
• package  directory (folder)
• class  file
• A class named D in package a.b.c should reside in this file:
a/b/c/D.class
 (relative to the root of your project)
• The "root" directory of the package hierarchy is determined by your
class path or the directory from which java was run.
4
Classpath
• class path: The location(s) in which Java looks for class files.
• Can include:
 the current "working directory" from which you ran javac / java
 other folders
 JAR archives
 URLs
 ...
• Can set class path manually when running java at command line:
 java -cp /home/stepp/libs:/foo/bar/jbl MyClass
5
A package declaration
package name;
public class name { ...
Example:
package pacman.model;
public class Ghost extends Sprite {
...
}
• File Sprite.java should go in folder pacman/model .
6
Importing a package
import packageName.*; // all classes
Example:
package pacman.gui;
import pacman.model.*;
public class PacManGui {
...
Ghost blinky = new Ghost();
}
• PacManGui must import the model package in order to use it.
7
Importing a class
import packageName.className; // one class
Example:
package pacman.gui;
import pacman.model.Sprite;
public class PacManGui {
Ghost blinky = new Ghost();
}
• Importing single classes has high precedence:
 if you import .*, a same-named class in the current dir will override
 if you import .className, it will not
8
Static import
import static packageName.className.*;
Example:
import static java.lang.Math.*;
...
double angle = sin(PI / 2) + ln(E * E);
• Static import allows you to refer to the members of another class
without writing that class's name.
• Should be used rarely and only with classes whose contents are
entirely static "utility" code.
9
Referring to packages
packageName.className
Example:
java.util.Scanner console =
new java.util.Scanner(java.lang.System.in);
• You can use a type from any package without importing it if you
write its full name.
• Sometimes this is useful to disambiguate similar names.
 Example: java.awt.List and java.util.List
 Or, explicitly import one of the classes.
10
The default package
• Compilation units (files) that do not declare a package are put into a
default, unnamed, package.
• Classes in the default package:
 Cannot be imported
 Cannot be used by classes in other packages
• Many editors discourage the use of the default package.
• Package java.lang is implicitly imported in all programs by default.
 import java.lang.*;
11
Package access
• Java provides the following access modifiers:
 public : Visible to all other classes.
 private : Visible only to the current class (and any nested types).
 protected : Visible to the current class, any of its subclasses, and
any other types within the same package.
 default (package): Visible to the current class and any other types
within the same package.
• To give a member default scope, do not write a modifier:
package pacman.model;
public class Sprite {
int points; // visible to pacman.model.*
String name; // visible to pacman.model.*
12
Package exercise
• Add packages to the Rock-Paper-Scissors game.
 Create a package for core "model" data.
 Create a package for graphical "view" classes.
 Any general utility code can go into a default package or into another
named utility (util) package.
 Add appropriate package and import statements so that the types can
use each other properly.
13
• JAR: Java ARchive. A group of Java classes and supporting files
combined into a single file compressed with ZIP format, and given
.JAR extension.
• Advantages of JAR files:
 compressed; quicker download
 just one file; less mess
 can be executable
• The closest you can get to having a .exe
file for your Java application.
JAR Files (yousa likey!)
14
Creating a JAR archive
• from the command line:
jar -cvf filename.jar files
 Example:
jar -cvf MyProgram.jar *.class *.gif *.jpg
• some IDEs (e.g. Eclipse) can create JARs automatically
 File → Export... → JAR file
15
Running a JAR
• Running a JAR from the command line:
 java -jar filename.jar
• Most OSes can run JARs directly by double-clicking them:
16
Making a runnable JAR
• manifest file: Used to create a JAR runnable as a program.
jar -cvmf manifestFile MyAppletJar.jar
mypackage/*.class *.gif
Contents of MANIFEST file:
Main-Class: MainClassName
 Eclipse will automatically generate and insert a proper manifest
file into your JAR if you specify the main-class to use.
17
Resources inside a JAR
• You can embed external resources inside your JAR:
 images (GIF, JPG, PNG, etc.)
 audio files (WAV, MP3)
 input data files (TXT, DAT, etc.)
 ...
• But code for opening files will look outside your JAR, not inside it.
 Scanner in = new Scanner(new File("data.txt")); // fail
 ImageIcon icon = new ImageIcon("pony.png"); // fail
 Toolkit.getDefaultToolkit().getImage("cat.jpg"); // fail
18
Accessing JAR resources
• Every class has an associated .class object with these methods:
 public URL getResource(String filename)
 public InputStream getResourceAsStream(String name)
• If a class named Example wants to load resources from within a
JAR, its code to do so should be the following:
 Scanner in = new Scanner(
Example.class.getResourceAsStream("/data.txt"));
 ImageIcon icon = new ImageIcon(
Example.class.getResource("/pony.png"));
 Toolkit.getDefaultToolkit().getImage(
Example.class.getResource("/images/cat.jpg"));
 (Some classes like Scanner read from streams; some like Toolkit read from URLs.)
 NOTE the very important leading / character; without it, you will get a null result
19
JAR to EXE (JSmooth)
• JSmooth is a free program that
converts JARs into Windows EXE files.
 http://jsmooth.sourceforge.net/
 If the machine does not have Java
installed, your EXE will help the user
to download and install Java.
 A bit of a hack; not generally needed.
• Using JSmooth:
 choose Skeleton → Windowed Wrapper
 name your .exe under Executable → Executable Binary
 browse to your .jar under Application → Embedded JAR
 select the main class under Application → Main class

More Related Content

Similar to 20-packages-jar.ppt

packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
pankaj chelak
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
VeenaNaik23
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
bhuvaneshwariA5
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
Vishnu Suresh
 
packages unit 5 .ppt
packages  unit 5 .pptpackages  unit 5 .ppt
packages unit 5 .ppt
thenmozhip8
 
Packages
PackagesPackages
Packages
Nuha Noor
 
Packages in java
Packages in javaPackages in java
Packages in java
Kavitha713564
 
Pi j4.1 packages
Pi j4.1 packagesPi j4.1 packages
Pi j4.1 packages
mcollison
 
Package in Java
Package in JavaPackage in Java
Package in Java
lalithambiga kamaraj
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
Kuntal Bhowmick
 
javapackage
javapackagejavapackage
javapackage
Arjun Shanka
 
Java Packages
Java Packages Java Packages
Packages in java
Packages in javaPackages in java
Packages in java
jamunaashok
 
Packages in java
Packages in javaPackages in java
Packages in java
Jancypriya M
 
Object Oriented Programming - Java
Object Oriented Programming -  JavaObject Oriented Programming -  Java
Object Oriented Programming - Java
Daniel Ilunga
 
Java package
Java packageJava package
Java package
CS_GDRCST
 
Packages in java
Packages in javaPackages in java
Packages in java
SahithiReddyEtikala
 
Java packages
Java packagesJava packages
Java packages
Jeffrey Quevedo
 
Unit3 packages & interfaces
Unit3 packages & interfacesUnit3 packages & interfaces
Unit3 packages & interfaces
Kalai Selvi
 
Java package
Java packageJava package
Java package
Arati Gadgil
 

Similar to 20-packages-jar.ppt (20)

packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
packages unit 5 .ppt
packages  unit 5 .pptpackages  unit 5 .ppt
packages unit 5 .ppt
 
Packages
PackagesPackages
Packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Pi j4.1 packages
Pi j4.1 packagesPi j4.1 packages
Pi j4.1 packages
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
javapackage
javapackagejavapackage
javapackage
 
Java Packages
Java Packages Java Packages
Java Packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Object Oriented Programming - Java
Object Oriented Programming -  JavaObject Oriented Programming -  Java
Object Oriented Programming - Java
 
Java package
Java packageJava package
Java package
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java packages
Java packagesJava packages
Java packages
 
Unit3 packages & interfaces
Unit3 packages & interfacesUnit3 packages & interfaces
Unit3 packages & interfaces
 
Java package
Java packageJava package
Java package
 

Recently uploaded

原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
ihavuls
 
The Ipsos - AI - Monitor 2024 Report.pdf
The  Ipsos - AI - Monitor 2024 Report.pdfThe  Ipsos - AI - Monitor 2024 Report.pdf
The Ipsos - AI - Monitor 2024 Report.pdf
Social Samosa
 
University of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma TranscriptUniversity of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma Transcript
soxrziqu
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
v7oacc3l
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
Social Samosa
 
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
taqyea
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
VyNguyen709676
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
jitskeb
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
Timothy Spann
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
vikram sood
 
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
mkkikqvo
 
A presentation that explain the Power BI Licensing
A presentation that explain the Power BI LicensingA presentation that explain the Power BI Licensing
A presentation that explain the Power BI Licensing
AlessioFois2
 
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
xclpvhuk
 
Open Source Contributions to Postgres: The Basics POSETTE 2024
Open Source Contributions to Postgres: The Basics POSETTE 2024Open Source Contributions to Postgres: The Basics POSETTE 2024
Open Source Contributions to Postgres: The Basics POSETTE 2024
ElizabethGarrettChri
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
sameer shah
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
manishkhaire30
 
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docxDATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
SaffaIbrahim1
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
nuttdpt
 
原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理
原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理
原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理
wyddcwye1
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
Sachin Paul
 

Recently uploaded (20)

原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
 
The Ipsos - AI - Monitor 2024 Report.pdf
The  Ipsos - AI - Monitor 2024 Report.pdfThe  Ipsos - AI - Monitor 2024 Report.pdf
The Ipsos - AI - Monitor 2024 Report.pdf
 
University of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma TranscriptUniversity of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma Transcript
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
 
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(harvard毕业证书)哈佛大学毕业证如何办理
 
writing report business partner b1+ .pdf
writing report business partner b1+ .pdfwriting report business partner b1+ .pdf
writing report business partner b1+ .pdf
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
 
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
 
A presentation that explain the Power BI Licensing
A presentation that explain the Power BI LicensingA presentation that explain the Power BI Licensing
A presentation that explain the Power BI Licensing
 
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
 
Open Source Contributions to Postgres: The Basics POSETTE 2024
Open Source Contributions to Postgres: The Basics POSETTE 2024Open Source Contributions to Postgres: The Basics POSETTE 2024
Open Source Contributions to Postgres: The Basics POSETTE 2024
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
 
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docxDATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
 
原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理
原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理
原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
 

20-packages-jar.ppt

  • 1. 1 CSE 331 Java Packages; JAR Archives slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/
  • 2. 2 Java packages • package: A collection of related classes.  Can also "contain" sub-packages.  Sub-packages can have similar names, but are not actually contained inside. •java.awt does not contain java.awt.event • Uses of Java packages:  group related classes together  as a namespace to avoid name collisions  provide a layer of access / protection  keep pieces of a project down to a manageable size
  • 3. 3 Packages and directories • package  directory (folder) • class  file • A class named D in package a.b.c should reside in this file: a/b/c/D.class  (relative to the root of your project) • The "root" directory of the package hierarchy is determined by your class path or the directory from which java was run.
  • 4. 4 Classpath • class path: The location(s) in which Java looks for class files. • Can include:  the current "working directory" from which you ran javac / java  other folders  JAR archives  URLs  ... • Can set class path manually when running java at command line:  java -cp /home/stepp/libs:/foo/bar/jbl MyClass
  • 5. 5 A package declaration package name; public class name { ... Example: package pacman.model; public class Ghost extends Sprite { ... } • File Sprite.java should go in folder pacman/model .
  • 6. 6 Importing a package import packageName.*; // all classes Example: package pacman.gui; import pacman.model.*; public class PacManGui { ... Ghost blinky = new Ghost(); } • PacManGui must import the model package in order to use it.
  • 7. 7 Importing a class import packageName.className; // one class Example: package pacman.gui; import pacman.model.Sprite; public class PacManGui { Ghost blinky = new Ghost(); } • Importing single classes has high precedence:  if you import .*, a same-named class in the current dir will override  if you import .className, it will not
  • 8. 8 Static import import static packageName.className.*; Example: import static java.lang.Math.*; ... double angle = sin(PI / 2) + ln(E * E); • Static import allows you to refer to the members of another class without writing that class's name. • Should be used rarely and only with classes whose contents are entirely static "utility" code.
  • 9. 9 Referring to packages packageName.className Example: java.util.Scanner console = new java.util.Scanner(java.lang.System.in); • You can use a type from any package without importing it if you write its full name. • Sometimes this is useful to disambiguate similar names.  Example: java.awt.List and java.util.List  Or, explicitly import one of the classes.
  • 10. 10 The default package • Compilation units (files) that do not declare a package are put into a default, unnamed, package. • Classes in the default package:  Cannot be imported  Cannot be used by classes in other packages • Many editors discourage the use of the default package. • Package java.lang is implicitly imported in all programs by default.  import java.lang.*;
  • 11. 11 Package access • Java provides the following access modifiers:  public : Visible to all other classes.  private : Visible only to the current class (and any nested types).  protected : Visible to the current class, any of its subclasses, and any other types within the same package.  default (package): Visible to the current class and any other types within the same package. • To give a member default scope, do not write a modifier: package pacman.model; public class Sprite { int points; // visible to pacman.model.* String name; // visible to pacman.model.*
  • 12. 12 Package exercise • Add packages to the Rock-Paper-Scissors game.  Create a package for core "model" data.  Create a package for graphical "view" classes.  Any general utility code can go into a default package or into another named utility (util) package.  Add appropriate package and import statements so that the types can use each other properly.
  • 13. 13 • JAR: Java ARchive. A group of Java classes and supporting files combined into a single file compressed with ZIP format, and given .JAR extension. • Advantages of JAR files:  compressed; quicker download  just one file; less mess  can be executable • The closest you can get to having a .exe file for your Java application. JAR Files (yousa likey!)
  • 14. 14 Creating a JAR archive • from the command line: jar -cvf filename.jar files  Example: jar -cvf MyProgram.jar *.class *.gif *.jpg • some IDEs (e.g. Eclipse) can create JARs automatically  File → Export... → JAR file
  • 15. 15 Running a JAR • Running a JAR from the command line:  java -jar filename.jar • Most OSes can run JARs directly by double-clicking them:
  • 16. 16 Making a runnable JAR • manifest file: Used to create a JAR runnable as a program. jar -cvmf manifestFile MyAppletJar.jar mypackage/*.class *.gif Contents of MANIFEST file: Main-Class: MainClassName  Eclipse will automatically generate and insert a proper manifest file into your JAR if you specify the main-class to use.
  • 17. 17 Resources inside a JAR • You can embed external resources inside your JAR:  images (GIF, JPG, PNG, etc.)  audio files (WAV, MP3)  input data files (TXT, DAT, etc.)  ... • But code for opening files will look outside your JAR, not inside it.  Scanner in = new Scanner(new File("data.txt")); // fail  ImageIcon icon = new ImageIcon("pony.png"); // fail  Toolkit.getDefaultToolkit().getImage("cat.jpg"); // fail
  • 18. 18 Accessing JAR resources • Every class has an associated .class object with these methods:  public URL getResource(String filename)  public InputStream getResourceAsStream(String name) • If a class named Example wants to load resources from within a JAR, its code to do so should be the following:  Scanner in = new Scanner( Example.class.getResourceAsStream("/data.txt"));  ImageIcon icon = new ImageIcon( Example.class.getResource("/pony.png"));  Toolkit.getDefaultToolkit().getImage( Example.class.getResource("/images/cat.jpg"));  (Some classes like Scanner read from streams; some like Toolkit read from URLs.)  NOTE the very important leading / character; without it, you will get a null result
  • 19. 19 JAR to EXE (JSmooth) • JSmooth is a free program that converts JARs into Windows EXE files.  http://jsmooth.sourceforge.net/  If the machine does not have Java installed, your EXE will help the user to download and install Java.  A bit of a hack; not generally needed. • Using JSmooth:  choose Skeleton → Windowed Wrapper  name your .exe under Executable → Executable Binary  browse to your .jar under Application → Embedded JAR  select the main class under Application → Main class