Friday, July 28, 2017

Creating a jar File in Eclipse

This page shows you how to create a jar file (java archive) using the Eclipse IDE. The instructions are for bundling the source code (.java files) into a jar. These instructions do not cover how to make an executable jar.
To create a new JAR file in the workbench:
  1. Either from the context menu or from the menu bar's File menu, select Export.



     
  2. Expand the Java node and select JAR file. Click Next.



     
  3. In the JAR File Specification page, select the resources that you want to export in the Select the resources to export field.




     
  4. Select the appropriate checkbox to specify whether you want to Export generated class files and resources or Export Java source files and resources. (Note: Selected resources are exported in both cases.

    IMPORTANT: In this example the project keeps the source code in a folder named src. Your project may have a different set up. Be sure you expand the tree to show the default package and that the .java files are checked on the right. Alternatively deselect the option that says Export generated class files and resources and select the option that says Export Java source files and resources. For CS307 we want the .java files. We do not want or need the .class files. If you do not turn in your source files, the .java files, your assignment grade will be 0.



    If there are other files or resources you want to include they must be in a an open project. Browse to their location via the directory tree on the left and ensure the file or resource is checked in the window on the right. In the example below we are including a file named  OtherFile.txt which is located in the A3 directory.



     
  5. In the Select the export destination field, either type or click Browse to select a location for the JAR file.






     
  6. Select or clear the Compress the contents of the JAR file checkbox. (This option is unimportant for CS307.)
  7. Select or clear the Overwrite existing files without warning checkbox. If you clear this checkbox, then you will be prompted to confirm the replacement of each file that will be overwritten. (This option is unimportant for CS307.)
  8. You have two options:
  9. Now, navigate to the location you specified for the jar. The icon you see and the behavior you get if you double click it will vary depending on how your computer is set up.



    One easy way of checking if the jar has the correct files is to rename it with a .zip extension. Then use whatever you zip program is to look at the files inside. (This may not work depending on how your system is set up.)

    Rename to .zip extension:



    Open up with zip program / utility.



    In the above image you can see that A3.jar (renamed to A3.zip) contains 2 files MathMatrix.java and MathMatrixTester.java. Realize you may have multiple copies of those files on your computer. You can unzip the file (in a location different from where the files were originally located!!) and open the .java files to ensure they are correct or compare the size of the files to the size of the originals. Alternatively you can unjar the jar file, again in a different location that the original .java files, and check the files.

    If you changed the extension to .zip you must change it back to .jar before submitting your file via turnin.

Adding an External Library to an Eclipse Project Folder

Java comes with a virtual swiss army knife of tools and classes -- but it doesn't have everything. You can always build what you need from scratch, but sometimes -- if you are lucky -- someone, somewhere has already built what you need, and will graciously let you use it! As such things often involve many, many related individual java classes, interfaces, etc... (collectively called a java library) they are often shared via archive files called JAR files (short for "java archive"). A JAR file is not unlike the ZIP files seen frequently in email attachments, in that it is one or more files that have been compressed into a single, "as-small-as-possible" file.
If you do find yourself fortunate enough to have located an external JAR file for a java library that would help you in one of your projects, it is a simple matter to add it to your project so that you might take advantage of it's contents...
To demonstrate how this might be done by example, let us suppose that we want to be able to quickly and easily write and deploy to web pages both console programs and programs with a graphical user interface. After some digging online, you discover the Association for Computing Machinery (ACM) has created a library of classes just for that purpose, and you download the corresponding acm.jar file to your local machine.
To add the functionality provided by this library to one of your projects, simply do the following:
  1. Start Eclipse, and locate the project folder to which this ACM library should be added.
  2. Right-click this class folder, and select "Properties"


  3. Select "Java Build Path" on the left, and then the "Libraries" tab. Now, click the "Add External JARS..." button


  4. Locate and select the "acm.jar" file you just downloaded, and then click "Open"


  5. Finally, click "OK" to close the dialog box. You will know that everything went ok if, when you open your project folder, you see an item called "Referenced Libraries", and upon expanding this item, you see the package "acm.jar" listed.

That's it -- simple, wasn't it!