What is JAR File and How to create JAR File

A JAR File, stands for Java Archive file. Like ZIP file, JAR is compressed file format, which is used to share software resources required for Java Applications. For example Hibernate, Struts, Spring,etc… provide their framework implementation through multiple JAR files. Similarly you can provide Jar files to your clients, or other Teams dependent on your Classes or packages. Even JDBC driver is provided as a Jar file. A Jar file may contain
1. bunch of .class files
2. any other resource files, like images, icons,etc… required by .class files

Also note that a jar file can have multiple packages or sub packages.

How to create JAR file in Eclipse

Below are the steps involved in creating jar file in Eclipse IDE.
1. Create a Java Project
2. Add class Files
3. Build the Project, do Testing
4. Right click on Project, and Select Export

export jar file
export jar file

5. In Export Window, select java, then JAR file, then Next button.
create-jar
create-jar

6. Now Below JAR File Specification window appears. Select various options, based on your requirement.
Select or unselect appropriate checkbox to specify if you want to Export generated class files and resources or Export Java source files and resources as well.

jar-file-specification
jar-file-specification

Browse to select a location for the JAR file, which will get created.

Select or unselect, Compress the contents of the JAR file checkbox.
Select or unselect, Overwrite existing files without warning checkbox. If you unselect this checkbox, then you will be prompted to confirm the replacement of each file that will be overwritten.

In this way you can create JAR file, out of your class files, and other required resources, and distribute it.

You may also like to read:

How to Compress and Uncompress file in Java


How to create a Jar file in a Netbeans Project?
How to create Jar file, in run time, using Java program?
How to add an external Jar file to a Eclipse Project?
How to add an external jar file to a Netbeans Project?

Purpose of a package, in Java

What is a package?

A Package is set of Classes, interfaces or sub packages.

Advantage of package

Below are advantages of packages
1. To avoid name collisions between classes or interfaces. Using different packages it is possible to have Classes or interfaces with same name.
2. Improves Modularity of Applications. As application development, is done across various Teams spread across various organizations, it is advantage to have each software module or layer in a package, and each sub module may further have sub package.
3. Another advantage of packages is, packages provides one more level of accessibility. Class members or classes with default or no access specifier, are accessible only within, current package.

All public classes and interfaces can be accessed by other packages also.
All classes and interfaces with default or none access specifier, can be accessed only within current package.

Below is diagram showing two packages com.java652 and com.p1. The package com.java652 has two classes WeatherDetails and CityDetails. The package com.p1 has one class StateDetails and an interface. objects of classes in different packages interact with each other, to perform required functionality.

What is Java Package
Java Package

Keywords used for packages





Below are keywords used in packages
1. package
package keyword is used to specify to which package, current java source file belongs to. There can be only package statement in a file. package statement need to be first statement in the file.
2. import
import keyword is used to specify that classes or interfaces of other packages need to be used in current java file. There can be multiple import statements, in a single file.

Also note that a separate folder is created with package name as folder name. And all class and interface files belonging to this package, are in this folder.

Download Package example(Eclipse Project)

For each sub package, one sub folder is created, under the main folder.

How to create package

how to create package, manually

When you develop java project using command line, and simple text editor, you need to manually create the package.
That means you need to manually create folders, and sub folders, for packages and sub packages, accordingly. Place java source file in corresponding folders or sub folders.

How to create package on Eclipse

As shown in below picture,
Right click on project, for which you want to create a package.
Now select New, then select package, then fill the package name like com.java652
To create class in this package, right click on package, select New, then select Class, then fill the class name.

how to create package in eclipse
how to create package in eclipse

How to create package on Netbeans

As shown in below picture,
Right click on project, for which you want to create a package.
Now select New, then select Java Package, then fill package name
To create class in this package, right click on package, select New, then select class.

how to create package in netbeans
how to create package in netbeans

you may also like to read:
How many Access specifiers in Java?
Can a abstract class have static method

How to install Java

Below are the steps involved in installing Java on Windows.

1. First find, if you are using Windows version is 32 bit or 64 bit.
2. Download and install JRE 32bit or 64bit(based on your Windows version), from below URL
http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html

What is JRE? JRE stands for Java Run time Environment, which comprises JVM(Java Virtual Machine) and Run time libraries required to run .class file i..e byte code.

How to Download JRE
Download JRE

3. Download and install JDK 32bit or 64bit(based on your Windows version), from below URL
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
How to Download JDK
Download JDK

What is JDK? JDK stands for Java Development Kit, which comprises tools required for developing and compiling Java Applications.
4.Set PATH environment variable
PATH need to be set, to access tools such as javac to be used any where on the installed system. If PATH is not set developer need to specify full absolute path
5.set CLASSPATH environment variable
6.Install IDE(Integrated Development Environment) of your choice. Also note that installing and setting up IDE is not compulsory. You can use IDE of your choice, Eclipse, Netbeans IDEs are being widely used for developing Java Applications.

How to Setup Eclipse for Java

1. Eclipse can be downloaded from below url
http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/junor
2. Unzip Eclipse downloaded zip file
3. Open eclipse.exe, whcih is Eclispe IDE.
NOTE: Eclipse is not installable setup file, instead it can be directly run, whenever you want to open Eclipse IDE.
4. To set common JRE, for all Java Projects
Select Preferences -> Java -> Installed JRE’s tab. Click Add, and follow on Screen instructions to complete adding JRE.
5. To set JRE for individual Java Projects in Eclipse IDE
a) Right click your project > properties
b) Select “Java Build Path” on left, then “JRE System Library”, click Edit…
c) Select “Workspace Default JRE”
d) Click “Installed JREs”
e) If you see JRE you want in the list select it (selecting a JDK is OK too)
f) If not, click Search…, navigate to Computer > Windows C: > Program Files > Java, then click OK
g) Now you should see all installed JREs, select the one you want and Click OK or Finish