Importing and Exporting Java Projects as JAR Files in Eclipse

One of the most convenient ways to archive a Java project is to store it in a JAR (Java ARchive) file. JAR files are a lot like zip files; they allow you to group a number of files together into one. JAR files are also the standard way of packaging projects to be run as applets within a browser. Eclipse allows you to both export projects into JAR files and to import a project from a JAR file.

To export a project to a JAR file

1.    Start Eclipse and navigate to your workspace.

2.    In Package Explorer, left-click on the project you want to export.

3.    Right-click on the same project and select Export�

4.    When the Export dialog box pops up, expand Java and click on JAR file. Click Next.

5.    The JAR Export dialog will pop up. Check the 2nd, 3rd, and 4th export check boxes as I have done for my project. Make sure that your project�s src folder is checked. Also, browse to create a name and find a location for your JAR file.

6.    Click Finish. Your JAR file will be created in the location you selected.

To import a project from a JAR file

1.    Start Eclipse and navigate to your workspace.

2.    Create a new project. The name of the project is not important to the import process.

3.    Expand the project name in Package Explorer and left-click on the src folder.

4.    Right-click on the src folder and select Import�

5.    In the Import dialog, expand General and select Archive File. Click Next.

6.    Browse for the JAR file that you want to import and click Open.

7.    Another Import dialog box opens and shows you what�s in your JAR file. Ideally, you should see one or more files with .java extensions. If you don�t, then the archive was not built correctly. Make sure that you are importing into the src folder of the project that you just created. Here is what my dialog looks like when I import the JAR file from the previous example into a project called ImportTest:

8.    Click Finish and the files should import correctly. Expand the src folder for your new project to make sure that everything is there.