- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
ECLIPSE IDE - Demistfying JAVA packages and jar files
What is a JAVA package?
A JAVA package in simplest terms is a collection of classes which are part of a JAVA project. All classes for a project can belong to a single package, however when a project is large and complex a strategy of organizing classes is generally implemented. Multiple packages are used to organize classes which are somehow related, perhaps by functionality or related to a specific application (such as a database application).
- ECLIPSE IDE - A Walk Through Creating the "Hello World!" Tutorial Application
ECLIPSE IDE for JAVA has a introductory way getting familiar with its use. Not unlike many programming languages it starts with a simple text
A Simple Package Study in ECLIPSE
Let's just take the simplest case of the Hello World! tutorial application in ECLIPSE.
If you followed my tutorial "ECLIPSE IDE - A Walk Through Creating the "Hello World!" Tutorial Application" or worked through the tutorial on you own. there is a point where you create the class HelloWorld. In creating the class, the screen presented has a entry for a package name (which is not required to be specified)..Since the tutorial did not indicate the need for an entry, if you followed the instructions explicitly, you didn't enter anything. A yellow caution symbol and message was raised when you clicked on FINISH.
You might have become aware of a warning message which appears when clicked FINISH. The message illustrated below says the use of a default package is discouraged and you wondered why.
Warning about Default Package Use
What is the difference between default and named packages?
Let's perform an experiment to demonstrate the differences. Let's assume that you have a project and need to create 3 classes. We will create the classes first without specifying a package name, then repeat the experiment creating the three classes in a packaged named HelloProgramPackage. Now let's look at the directory structure in each case.
Classes Created in Default Package
Classes Created in a Named Package
Packages and jar files
Specifying a package name when you create classes help to segregate associated related files. For a complex application there may be classes with different functionality. When creating them it would be best to keep them separate. Also, it would segregate class files form other files which might be created in the src sub-directory of the project sub-directory of the workspace. For now, for our purposes of learning ECLIPSE the best practice is to always create a class in a named package. In our next lesson, we will look at the standard, formal naming of packages.
Another advantage has to do with the philosophy of using an IDE such as ECLIPSE. Rather than creating all of code for an application from scratch, code can be shared and reused. A well defined and documented package can be distributed.
So, where do Jar files come into play?
As a package can become quite large, a convenience for distributing and using them is by means of a jar file. The jar extension to the file name stands for Java ARchive. jar fiiles are compressed files similar to zip files. They can be created (compressed) or extracted (uncompressed).
More on .jar Files
You can open a command prompt window and type jar to see if it is on your path. if you get a error message you can set up your environment by performing the following steps:
- Click on Computer from the Start
- Select Properties
- Select Advanced Setting
- Click the Environment Variables button
- Under System Variables click New
- Enter the variable name as JAVA_HOME
- Enter the path to the JDK (Java Development Kit)
- Click OK
- Click Apply Changes
Start a command prompt and type jar.
Use the two following illustrations as an outline.
Setting Up the JAVA_HOME Environmental Variable
Running jar from the Command Line
Wrap-Up and What's Next
In this tutorial we have discussed creation of packages and given a hint as to why named package specification is a best practice. The definition of a jar file given and the procedure for setting up JAVA_HOME will aid in the use of the jar executable in future.
In a previous tutorial we discussed the file structure some what in depth. In the next tutorial we will take a closer look at both the "anatomy" of both the ECLIPSE product and the developer's workspace.