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

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.
Advertisements

Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Processing and Java David Meredith
Packages Sudhir Talasila Preeti Navale. Introduction Packages are nothing more than the way we organize files into different directories according to.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
CSE 303 Lecture 16 Multi-file (larger) programs
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
Computer Science A 13: 31/3. From.java to.exe - classpath - jar - resources - wrapper/launcher.
1 Applets. 2 Motivations When browsing the Web, you frequently see the graphical user interface and animation developed using Java. These programs are.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
1 Chapter 3 Programs and Packages. 2 Java Virtual Machine (JVM) Java programs execute on the JVM. The JVM is a virtual rather than a physical machine,
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
1 Introduction to Tool chains. 2 Tool chain for the Sitara Family (but it is true for other ARM based devices as well) A tool chain is a collection of.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
PACKAGES Mimi OpkinsCECS277. What We’ll Cover  Packages and Import Statements  The Package java.lang  Package Names and Directories  The Default Package.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Intro. to Game Programming Want to program a game?
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
1 Lecture 2: Object Oriented Programming in Java.
Using Eclipse. What is Eclipse? The Eclipse Platform is an open source IDE (Integrated Development Environment), created by IBM for developing Java programs.
Object Oriented Programming in Java Habib Rostami Lecture 5.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
UCSC All rights reserved. No part of this material may be reproduced and sold. 1 IT1202-Fundamentals Of Programming (Using JAVA) Interacting with.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Appendix E The EZJava.
Datalogi A 16: 21/11. Java Play sounds in applications Compiling to jar-files Compiling to exe-files Exceptions Sorting, reading from a file, writing.
The Basics of Javadoc Presented By: Wes Toland. Outline  Overview  Background  Environment  Features Javadoc Comment Format Javadoc Program HTML API.
Introduction to Eclipse CSC 216 Lecture 3 Ed Gehringer Using (with permission) slides developed by— Dwight Deugo Nesa Matic
Packages. Package 1.Avoid naming conflicts. Put classes into packages so that they can be referenced through package names. 2.Distribute software conveniently.
Java programming Package. A package is a group of similar types of classes, interfaces and sub-packages. Package can be categorized in two form, built-
EE2E1. JAVA Programming Lecture 3 Java Programs and Packages.
Ant & Jar Ant – Java-based build tool Jar – pkzip archive, that contains metadata (a manifest file) that the JRE understands.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
DOS and the Command Line CS 21a: Introduction to Computing I First Semester,
(C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Building Packages BCIS 3680 Enterprise Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Jar Files The Basics. 2 Outline Create a Jar Viewing the Contents of a JAR File Extracting the Contents of a JAR File Modifying a Manifest File Running.
Packages. Access Specifications Public Available anywhere (public keyword) Only one public class per file allowed Protected Available in subclasses, and.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Working with Packages BCIS 3680 Enterprise Programming.
Java Package Advantage of Java Package
UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 2
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Object-Oriented Programming with Java
Packages When we name a program , we name it by class name…
Java Basics Packages.
Packages and Interfaces
Applying OO Concepts Using Java
Using Eclipse.
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
CMPE212 – Reminders Assignment 2 due today, 7pm.
Presentation transcript:

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

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.   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