© 2001 by Ashby M. Woolf Revision 2 A First Look at CLASSPATHs, Packages and Import Statements How a Java Class Locates The Other Classes it Needs.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Introduction to Java 2 Programming Lecture 4 Writing Java Applications, Java Development Tools.
Design By Contract Using JMSAssert.
Packages Sudhir Talasila Preeti Navale. Introduction Packages are nothing more than the way we organize files into different directories according to.
Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.
•7/12 /07 F-1 © 2010 T. Horton CS 4240 Principles of SW Design Packages in Java and UML.
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.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Installing JDK Vijayan Sugumaran Department of DIS Oakland University.
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,
Packages Where, oh where, is my little lost class file?
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.
COMPILING JAVA PROGRAM USING JDK COMMAND LINE WINDOWS PLATFORM.
עיצוב תוכנה מונחה עצמים תירגול 1. 1.Packages and Paths 2.Jar Files Outline.
Shorthand operators.
PACKAGES Mimi OpkinsCECS277. What We’ll Cover  Packages and Import Statements  The Package java.lang  Package Names and Directories  The Default Package.
Access Control Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Packages F Package is a container for classes F A package is a grouping of related types (classes and interfaces) providing access protection and name.
1 Lecture 2: Object Oriented Programming in Java.
Dale Roberts Object Oriented Programming using Java - Packages Dale Roberts, Lecturer Computer Science, IUPUI Department.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
July 2011CMSC 341 CVS/Ant 1 CMSC 341 Java Packages Ant CVS Project Submission.
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-
4/1/05F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Packages and Components in Java and UML.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
Setting up Cygwin Computer Organization I 1 May 2010 ©2010 McQuain Cygwin: getting the setup tool Free, almost complete UNIX environment emulation.
1 JAVA API & Packages Starring: Java Documentation Co-Starring: BlueJ IDE.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
Installing JDK Vijayan Sugumaran Department of DIS Oakland University.
Even More Java. Java Packages What is a package? Definition: A package is a grouping of related types providing access protection and name space management.
22-July-2002cse142-13B-Development © 2002 University of Washington1 Development Tools CSE 142, Summer 2002 Computer Programming 1
Java Programming, Second Edition Appendix A Working with Java SDK 1.4.
CSS446 Spring 2014 Nan Wang.  A Java program consists of a collection of classes.  Package is a set of related classes. 2.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Henrik Bærbak Christensen1 Build Management Clean environment that works...
Building Packages BCIS 3680 Enterprise Programming.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Package A package is a logical container that contains classes,interfaces sub packages. Package provide a unique name space to its members and provide.
Packages. Access Specifications Public Available anywhere (public keyword) Only one public class per file allowed Protected Available in subclasses, and.
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.
Slide 1 Running NEMO at ECMWF Slide 1 NEMO under Perforce at ECMWF Kristian S. Mogensen Last revised:
Working with Packages BCIS 3680 Enterprise Programming.
Java Package Advantage of Java Package
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Object and Classes อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 3.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Java Packages - allow you to group classes as a collection Standard Java library has packages: java.lang and java.util … Also hierarchical: java and javax.
Foundations of Programming: Java
Packages In Java.
Packages When we name a program , we name it by class name…
Installing and running the local check projects in Eclipse
Java Basics Packages.
Packages and Interfaces
Executable Jars Dan Fleck Fall /27/2018.
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
Joel Adams and Jeremy Frens Calvin College
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Presentation transcript:

© 2001 by Ashby M. Woolf Revision 2 A First Look at CLASSPATHs, Packages and Import Statements How a Java Class Locates The Other Classes it Needs

© 2001 by Ashby M. Woolf Revision 2 Rule #1 A file may define any number of Java classes. Only one of the defined Java classes can be a public class. (Class order doesn't matter) Example: class One { // Class One stuff } public class Two { // Class Two stuff } class Three { // Class Three stuff }

© 2001 by Ashby M. Woolf Revision 2 Rule #2 The name of the a file defining one or more Java classes must be the name of the public class (if there is one) followed by.java Example: class One { // Class One stuff } public class Two { // Class Two stuff } class Three { // Class Three stuff } must be in a file named Two.java

© 2001 by Ashby M. Woolf Revision 2 Rule #3 If I compile a Java source code file ".java" get a class file for every class in the file Example - Compiling: class One { // Class One stuff } public class Two { // Class Two stuff } class Three { // Class Three stuff } Produces the class files: –One.class –Two.class –Three.class

© 2001 by Ashby M. Woolf Revision 2 Observations Java can find all classes by looking at file names Looking for.class files everywhere will not work Where should Java look?

© 2001 by Ashby M. Woolf Revision 2 What is Needed We want to be able to store collections of classes (.class files) in one or more trees that make sense. We want to be able to work with several collections of classes and move them about when necessary. We want each class to be able to specify where to look in a collection (tree) to find the other classes it wants to use. We don't want to change our program (class) if a collection (tree of.class files) is moved.

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH CLASSPATH is an environment variable that specifies the location to look for the top of a tree expected to contain.class files Example CLASSPATH=C:\b;C:\d\m Java would first look in the directories below C:\b If the class being sought was not there it would look in the directories below C:\d\m

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH Cont. We can store different libraries of.class files where we need to put them and just add them to the CLASSPATH. We can move Libraries and change the class path to suit. We can run different versions of libraries by changing the CLASSPATH

© 2001 by Ashby M. Woolf Revision 2 Why the import Statement? Don't want to look in every directory in a large library –May find the a class with the same name in two places –Waste lots of time wandering around looking –Have no way to merge names from separately developed libraries without changing some names For every directory in the CLASSPATH Java only looks in one subdirectory for the class file being sought.

© 2001 by Ashby M. Woolf Revision 2 A Fully Qualified Class Name Example public class Foo { com.ajax.Bar b = new com.ajax.Bar(); ) Running in an environment with a CLASSPATH=C:\d\m Java would look for the Bar class specifically in the directory C:\d\m\com\ajax ( for the file Bar.class)

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abcd ijlmn compq xajaxz Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\d\m public class Foo { com.ajax.Bar b = new com.ajax.Bar(); } Fe. class Fi. class gov irs Bar.class javac

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n compq xajaxz Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\d\m public class Foo { com.ajax.Bar b = new com.ajax.Bar(); } Fe. class Fi. class gov irs Bar.class javac

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\d\m public class Foo { com.ajax.Bar b = new com.ajax.Bar(); } Fe. class Fi. class gov irs Bar.class javac

© 2001 by Ashby M. Woolf Revision 2 The import Statement The import statement are found at the beginning of a Java source file (a.java file). Example import com.ajax.Bar public class Foo { Bar b = new Bar(); ) Running in an environment with a CLASSPATH=C:\d\m Java would look for the Bar class specifically in the directory C:\d\m\com\ajax ( for the file Bar.class)

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ a b c d ijgovl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class javac irs Bar.class

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n compq xajaxz Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class gov irs Bar.class javac

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class gov irs Bar.class javac

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ a b c d ijgovl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class javac irs Bar.class

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ a b c d ijgovl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class irs Bar.class javac

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ a b c d ijgovl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class irs Bar.class javac

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n compq xajaxz Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class gov irs Bar.class javac

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class gov irs Bar.class javac

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m Fe. class Fi. class gov irs Bar.class javac import com.ajax.* public class Foo { Bar b = new Bar(); }

© 2001 by Ashby M. Woolf Revision 2 What is a Package All the class files in the same directory called a package or or said to be part of the same package. When we write: –import com.ajax.* –We say we have imported the package com.ajax

© 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m Fe. class Fi. class gov irs Bar.class javac import com.ajax.* public class Foo { Bar b = new Bar(); } The Package The Package Name

© 2001 by Ashby M. Woolf Revision 2 Keeping Libraries Separate Java programs are frequently stored in trees that begin with reversed domain name sequences. Example: –edu/tamu/math/util/* –edu/utexas/math/util/* Avoids conflicts

© 2001 by Ashby M. Woolf Revision 2 Imported by Default The package " java.lang.*" is imported by default Imports a minimal package of Java classes

© 2001 by Ashby M. Woolf Revision 2 A Simple Constraint com.ajax in the import statement expects the directory structure \com\ajax As a Result you can't have ". "s in the directory names of a tree of Java classes

© 2001 by Ashby M. Woolf Revision 2 Exercise Check your Sun Documentation and find out how many "Packages" are listed

© 2001 by Ashby M. Woolf Revision 2 End of Content