1 / 28

Working with Packages

Working with Packages. BCIS 3680 Enterprise Programming. Using Classes in JSP. This set of slides shows how to create a Java package and compile class files in the package. The following information is particularly important:

amable
Download Presentation

Working with Packages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Working with Packages BCIS 3680 Enterprise Programming

  2. Using Classes in JSP • This set of slides shows how to create a Java package and compile class files in the package. • The following information is particularly important: • How the folder hierarchy in Java development folder must mirror the package name hierarchy. • How the package name hierarchy is reflected in the java command.

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

  4. 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.Registration edu.smu.Registration

  5. package Keyword • To include a class in a package, precede the class definition with the packagestatement: 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;

  6. 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. Refer to Tutorial 1 for compilation instructions.

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

  8. 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…

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

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

  11. Packages • In this example, I created two packages in the project: edu.unt and wu.andy. • The edu.unt package contains the “driver” class named DateApp. • The wu.andy package contains a class named DateHandler. • The DateApp class reuses code from DateHandler by importing it.

  12. Contents of Source Code Folders • The DateApp.java file is stored in the unt subfolder of the edu folder. • If you’re not typing the code from scratch, copy the file into this location.

  13. Contents of Source Code Folders • The DateHandler.java file is stored in the andy subfolder of the wu folder. • If you’re not typing the code from scratch, copy the file into this location.

  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.

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

  16. Contents of Compiled File Folders • The compiled DateApp class is stored in build\classes\edu\unt.

  17. Contents of Compiled File Folders • The compiled DateHandler class is stored in build\classes\wu\andy.

  18. Manual Method • If you write source code outside of NetBeans, you may have to create the proper folder hierarchy for the source code. • 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.

  19. Creating Folder Hierarchy Manually • First, create the source code branch (\source\...) with all the subfolders. • Create only the top level folder for the compiled files (\classes).

  20. Creating Folder Hierarchy Manually • The commands in the previous slide created the folder hierarchy as shown below:

  21. Storing the Source Code When you write source code, store the .java files under the proper subfolders under \source (as in the case of using NetBeans). The only difference is that here you do it manually.

  22. Do the Same for All Source Code Files

  23. Principles in Compiling • These principles are marked with their respective numbers in purple circles like this in the example that follows. • Principle #1: When you run javac, if your working directory is where the source code (the .java file) is, then there is no need to specify the path to it because it’s “visible” in the directory. • Otherwise, specify the complete path to it. • Principle #2: Store the compiled files in \classes folder or proper subfolders of \classes if packages are used. This can be done more efficiently if at compile time, you specify where the .class file should be stored, using the –d flag. • javac –d <path to classes folder> <path to source file> 2

  24. Principles in Compiling • Principle #3: If ClassB needs ClassA in order to run, • Compile ClassA before you compile ClassB; and • Change your working directory to where the compiled ClassA (ClassA.class) is before compiling ClassB. • Since the source code of ClassA (ClassA.java) is not “visible” in this directory, you need to specify the complete path to ClassA.java.

  25. Compiling Source Code • In this example, compile the DateHandler class first because the DateApp class uses it. • The argument after –d is the path to the classes folder. • Since we are currently in the \source\wu\andy folder, the DateHandler.java file is visible in this location. So we don’t have to specify the path to the file. The file name alone suffice. • Note that if the javac command executed successfully, there is no output at the command line. 3 2 1 1 2 1

  26. Result of Manual Compilation • Notice that the javac command automatically found the \classes folder at the same level as \source and mirrored the \wu\andy folder hierarchy under it.

  27. Compiling Source Code • Since the DateApp class uses DateHandler, when we compile DateApp.java, we must first move to \classes, so that javac can locate the DateHandler.class we compiled earlier. • Since we’re running javac from \classes, the complete path to DateApp.java is needed. 3 Each ..\ moves us up one level in the folder hierarchy. Therefore, from the starting location, ..\..\..\ moves us to the root of the MyPackages folder. Append to it classes then moves us to the classes subfolder. 3 2 1

  28. Result of Manual Compilation • Again, the javac command mirrored the \edu\unt folder hierarchy under \classes.

More Related