Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The process of packaging a Java application for deployment is radically different from one platform to the next. You don't package a console application and a web application the same or a mobile application or a desktop application, they each have their own rules, but the one thing that all Java platforms have in common is the use of JAR files. A JAR file is a Java archive file, a compressed file in zip format that contains compiled Java classes, but has a file extension of .jar. I'm going to show you how to create a jar file from within Eclipse.
You can also do the same thing from the command line using the JAR command from the JDK. I'm working in a version of my application called CreateJAR, and the first thing I'll do is Run the application and make sure that it runs correctly. This version of the application outputs the names of the olives that I'm crushing and how many units of oil I have and where the first all of this from. I do this before I try to package the application to make sure I'm running valid code. Now, to create the JAR File, go to the menu and choose File, Export.
In the Export dialog choose Java, JAR File and click Next. Make sure you've selected your entire application including all of the applications packages, but deselect the options for the class path and project files on the right. These are Eclipse configuration files that you don't need to include in your deployed JAR File. The first option here in this list should be selected, Export generated class files and resources. For a console application that doesn't include any graphical assets or other files, you can deselect everything else.
Next, indicate the location and name of your JAR file. I have already filled in the value with my project name and the name of the JAR file I want to create, separated with a slash character, use a backslash on Windows and a forward slash on Mac, alternatively you can click Browse and select a location anywhere on your disk to create the file. I am creating my JAR File in my project folder, Create JAR and I'm giving it a name of olivepressapp.jar. You can name the file anything you want, just make sure you don't include any spaces or special characters, but you must use a .jar extension.
I've selected the option to compress the contents of the JAR File and deselected the other options. I'll click Next and show you these two options. For debugging I recommend selecting these options, Export class files with compile errors and warnings. For final production, you might want to deselect these options. And on this final screen, there are options for the manifest file. A manifest file is a text file that's automatically included in your JAR. It includes metadata about your application, but again, for a simple console application, you can accept all of these default values, click Finish and your new JAR File will be created.
If you created it in your project folder as I did, it should show up right away in the package Explorer. Now let's take a look at the contents of the JAR File. I'll go to my desktop to my Exercise Files folder, from there I'll go down to 12 Deployment to the Create JAR Folder and here's the .jar file that I just created. I'm going to copy it and paste it so that I create a new version, and then I'm going to rename this version of the file and give it a file extension of .zip. This will allow me to easily extract its contents, so I can see what's inside.
Next, I'll extract its contents. On Windows, I'll right-click and choose Extract All and Extract. On Mac, just double-click on the file. If you have any special archive utilities installed on your system, such as WinZip on Windows, you can extract the file with that tool. Here is the contents of the JAR File, there is a folder called MetaINF in and within that the manifest file. I'll open that up with a text editor and show you that for a simple console application like this, it's pretty much empty.
Now I'll go back to the main folder and show you that your packages have been translated to folders within the JAR file. Here is com, Lynda, olivepress, my main package. Here is my main compiled class and then within the press package, there is olivepress.class and within the olives package my three olive classes. So that's all a JAR File is. It's a zip file; it has a file extension of .jar, and its purpose is to package up your application for deployment.
If you're using Java in another development environment such as say Android, take a look at the documentation for that development environment to learn about deployment options for that platform, regardless of how you're deploying your Java application, JAR Files are almost certainly going to be involved.
Get unlimited access to all courses for just $25/month.Become a member