building packages bcis 3680 enterprise programming

21
Building Packages BCIS 3680 Enterprise Programming

Upload: augustus-webster

Post on 08-Jan-2018

233 views

Category:

Documents


0 download

DESCRIPTION

Name Your Package 3  To avoid naming conflicts between packages created by two programmers, Sun Microsystems recommended this naming convention:  Reverse the organization’s domain name and put it in front of each package.  E.g., both of the following two packages contains a class with the name Registration. However, since the domain names unt.edu and smu.edu are guaranteed to be unique, the package names thus generated are unique as well. Therefore, there won’t be confusion as to which Registration class is being referred to. edu.unt.Registration edu.smu.Registration

TRANSCRIPT

Page 1: Building Packages BCIS 3680 Enterprise Programming

Building Packages

BCIS 3680 Enterprise Programming

Page 2: Building Packages BCIS 3680 Enterprise Programming

2

Packages A package is a collection of related classes

that can be imported into a program. Packages allow reuse of classes without

having to place them in the same directory as the application currently being developed.

Putting your classes into a package reduces the chances of naming conflicts with other people’s classes.

Page 3: Building Packages BCIS 3680 Enterprise Programming

3

Name Your Package To avoid naming conflicts between packages created

by two programmers, Sun Microsystems recommended this naming convention: Reverse the organization’s domain name and put it in front

of each package. E.g., both of the following two packages contains a

class with the name Registration. However, since the domain names unt.edu and smu.edu are guaranteed to be unique, the package names thus generated are unique as well. Therefore, there won’t be confusion as to which Registration class is being referred to. edu.unt.Registrationedu.smu.Registration

Page 4: Building Packages BCIS 3680 Enterprise Programming

4

package Keyword To include a class in a package, precede the class

definition with the package statement: package packageName;

It must be the first statement in the source code file. It must be above any import statements. The folder hierarchy of the source code folder(s) must

agree with the package name. \edu\smu for the edu.smu package \edu\unt\cob for the edu.unt.cob package \foo for the foo package

To use the class in another package, import the class: import packageName.ClassName;

Page 5: Building Packages BCIS 3680 Enterprise Programming

5

Make Your Code Available If you want to make a package available to a

single application, you can just copy the directories and files of the package into the application’s directory.

If you want to make a package available to any application, you start by creating a Java Archive (JAR) file that contains the class files of the package.

Then you can move the JAR file into the JDK’s \jre\lib\ext folder or import it to the project’s library in NetBeans.

Page 6: Building Packages BCIS 3680 Enterprise Programming

6

Steps in Creating JAR Prepare the source files Create the right folder structure Compile the files Create the manifest file Run Jar command

Page 7: Building Packages BCIS 3680 Enterprise Programming

7

Preparing Class Files for Use Source code must be compiled before it can

be executed. The files that are fit for execution are

the .class (compiled) files, not the .java source code files.

You should be able to compile files and store the .class files in folders under proper folder hierarchy.

Using NetBeans can make this process easier. If you prefer to write source code and compile it outside of NetBeans, you may have to create the source code folder hierarchy manually.

Page 8: Building Packages BCIS 3680 Enterprise Programming

8

Using NetBeans If you use NetBeans, it

creates folders automatically for you while you create new packages and build (compile) files in the IDE.

The folder hierarchy in the source code folder and compiled file folder are mirrored without your intervention.

Page 9: Building Packages BCIS 3680 Enterprise Programming

9

Creating a Package in NetBeans To create a package in NetBeans, first create a

project. Then, right-click the project name or Source Package

(shown below) and select New | Java Package…

Page 10: Building Packages BCIS 3680 Enterprise Programming

10

Folders, Folders… For each of the levels in the package name (separated

by a dot), NetBeans adds a new child folder. In the example, the package name edu.unt is mirror in the

folder structure – edu\unt. If your IDE doesn’t do this automatically like NetBeans

does, you must create this folder structure manually.

Page 11: Building Packages BCIS 3680 Enterprise Programming

11

Contents of src Folder

As you create classes in the edu.unt package, for each new class, a corresponding .java file will appear in this folder.

Page 12: Building Packages BCIS 3680 Enterprise Programming

12

Create New Class in Package Add new classes to packages. If you’re not typing the code from scratch, copy the file into

this location. Remember the package statement must contain the correct

package name.

Page 13: Building Packages BCIS 3680 Enterprise Programming

13

Contents of Source Code Folders The DateAssistant.java and Driver.java

files are stored in the unt subfolder of the edu folder.

Page 14: Building Packages BCIS 3680 Enterprise Programming

14

Compiling Source Code To generate the binary, compiled version of the classes

(.class files), right click the project and select Build. After this is done, in the project folder, you will see a new

subfolder with the name build and under it, a classes subfolder. Under the classes subfolder, the same folder hierarchy under src is reproduced. However, these folders under classes store the .class files.

Page 15: Building Packages BCIS 3680 Enterprise Programming

15

Contents of Compiled File Folders The compiled DateAssistant and Driver

classes are stored in build\classes\edu\unt.

Page 16: Building Packages BCIS 3680 Enterprise Programming

16

Contents of classes Folder

Note the newly-created build folder and how the folder hierarchy under the classes folder mirrors that under the src folder.

Page 17: Building Packages BCIS 3680 Enterprise Programming

17

JAR File NetBeans also automatically creates the JAR file

for the project in the dist subfolder.

Page 18: Building Packages BCIS 3680 Enterprise Programming

18

Manual Method If you write source code

outside of NetBeans, you may have to create the proper folder hierarchy manually.

However, you don’t have to have the build folder.

For the folders to store compiled files, manually create only the classes folder.

javac is the command to compile source code. The –d switch of the command

allows you to specify where to store the .class files.

Page 19: Building Packages BCIS 3680 Enterprise Programming

19

The –d Flag javac –d <path to classes folder> <path to source file>

The –d flag tells the compiler to put the compiled code (.class files) into the classes directory.

The classes in a package should be saved in a subdirectory with a path that matches the name of the package.

Page 20: Building Packages BCIS 3680 Enterprise Programming

20

The manifest.txt File It marks the location of the main() method. Contains just one line:Main-Class: edu.unt.Driver Make sure you hit Return

This file must be placed right under the “classes” folder.

Page 21: Building Packages BCIS 3680 Enterprise Programming

21

JAR File Lets you pack all your classes into a single

file. Make sure all your class files are inside the

classes directory. Creating JAR file

Change your working directory into the class directory.

Run jar commandjar –cvmf manifest.txt myapp.jar *.class edu