Packages

Packages

As you incorporate third-party class increases the possibility to find classes with the same name

The possibilities are even greater in Java, where classes of millions of programmers have to travel through the network

The solution that provides Java are the packages (packages)

To specify the desired class simply indicate the name of the package to which it belongs

Sun gave in its day some recommendations for the names of the packages were unique:

  • If you are going to use within the company will be able to use any convention internal

  • If you are going to use outside the company should be used as a prefix in the Internet domain of the company invested (this ensures it is unique), for example com.sun.graphics3D

  • The standard classes are reserved for prefixes java and javax

The entire class must belong to some package

The Java classes are distributed in several basic packages:

Package
java javax
java.applet javax.accessibility
java.awt javax.activation
java.beans javax.activity
java.io javax.annotation
java.lang javax.crypto
java.math javax.imageio
java.net javax.jws
java.nio javax.lang
java.rmi javax.management
java.security javax.naming
java.sql javax.net
java.text javax.print
java.util javax.rmi
javax.script
javax.security
javax.smartcardio
javax.sound
javax.sql
javax.swing
javax.tools
javax.transaction
javax.xml

The name of a class includes a prefix of the package to which it belongs

In the example, the Vector class is inside the java package.util

In the example we have used the reserved word import that allows you to indicate the package to which belongs one or more classes, in such a way that you didn't have to repeat it with each use of the same

import does not load any type of file (not equivalent to #include in C / C++)

The package java.lang it is imported automatically

Collision of names

In the case of a repeat of a class name in two imported packages, we would have to use the full name of the class to undo the ambiguity

In the example you have specified the package that you want to take the Vector class, with the asterisk you're saying that you take all the package

In the example you have used an import of the concrete class Vector and another of all the package

Keep in mind that the import of the class will have priority over the import of an entire package

So it could be removed if you do not want to use the rest of the classes of that package, to be redundant

Creation of packages

To indicate that a class belongs to a package, use the clause package

In the example, we have created the package examples contains only the class Example1

If we are to use in another class, the name of the package may be formed by multiple words separated by dot

In this case the class name will be examples.Example1

If you would like to run it to use it in other classes from the command line:

Classpath

Java does not have the concept of executable file (file with extension .EXE or .COM)

Each class generates a file class independent

To run a program you need to specify the name of a class that contains at least a main function

The rest of the classes will be loaded dynamically on demand

You can have multiple classes with main but there can only be one main that is the main

In the case of local applications you will locate the code of the classes:

  • The JVM adds the name of the class the extension class to form the name of the file

  • To locate this file using the environment variable CLASSPATH

  • CLASSPATH indicates a series of locations in which to find the classes that can be:

    • A directory
    • A file ZIP or JAR (Java Archive Resource)

In the example we have used Windows command line to define the CLASSPATH

The class Test would look for (in this order):

  1. c:\examples\Test.class
  2. inside the file
  3. c:\java\lib\classes.zip and nowhere else

When using classes made by third parties it is necessary to include its directory (or ZIP, JAR) in the CLASSPATH for the JVM to be able to find them

Packages and Classpath

In the JDK there is a relationship between the package of a class and the directory in which it is located

All the classes in a package must be in a directory with the same name

If a package name has multiple words, each word corresponds with the name of a subdirectory

For example, the classes in the package ur.my.examples would be in the directory ur/my/examples

This directory must be accessible from any of the locations CLASSPATH

The mechanism of the JVM of the JDK to locate the implementation of a class at runtime is:

  1. adds .class the full name of the class
  2. transforms the points into directory separators \
  3. adds the resulting string to each of the locations CLASSPATH
  4. if it is not found in any of them produces a runtime error

Let us follow the process with an example in which we want to load in the JDK class UR.examples.example1:

  1. We define the CLASSPATH as:

  2. The JVM searches for the class in the file UR\examples\example1.class

  3. The file will be searched in the locations of the CLASSPATH c:\programs\UR\examples\example1.class and c:\temp\UR\examples\example1.class

  4. If the class is not found in either of these two sites, will result in a runtime error