Since I have received a few questions about it, I will detail how to create executable .jar files in eclipse.
Now there are two ways (that I know of) for creating .jar files in eclipse. The first one involves using the default .jar file creation options. Note that I am currently running eclipse version 3.5 Galileo, so other versions may look slightly different but should still have similar options.
First thing you need is to create your project with all the classes and libraries you need. Next, right click on your project and select “Export”. A pop-up menu should appear. In that, click on the “JAR file” option under the “Java” folder, then click Next. You should then see a window like the one below.
On the left side of the window, you should see your current projects. Make sure the one you want is selected. You can expand it to make sure that your src folder (with your java classes) and your lib folder (if needed) is selected. As for the check boxes at the bottom, you will want to at least have the ones selected that I do in the image. Namely the “Export generated class files and resources” and “Compress the contents of the JAR file” boxes. If you want, you can also select the “Export Java source files and resources” box. This will include the .java files in the .jar file so that you can look at them if you expand your .jar file. These are much easier to read than the .class files, but including them will increase the size of your .jar file. You can also select the location where you want to place the .jar file by clicking on the “Browse” button. After you finish that, click on Next to see this window:
In this box, the main thing you may want to check is the “Save the description of this JAR in the workspace” box. If you do this, a .jardesc file will be added to your project in eclipse. Later, you can simply right click on that .jardesc file and select “Create JAR” to rebuild the jar file without having to go through all these menus again. Click on next again.
It also is a good idea to create a manifest file if you don’t have one already. These files act as the driving file of the .jar file, telling it where to start the project, how to access the different files/libraries, etc. If you want to learn more about manifest files, you can check out this site. You also definitely want to make sure you specify which class has the main method that runs the project (as in the “public static void main(String args)” method). If you don’t do this, the .jar file will have no idea how to start the project when you run it. When all that is set, click on Finish. You now have yourself a jar file.
Unfortunately, this .jar file isn’t really useful for running an application outside of eclipse. You can try, but it will not recognize any non-native libraries you have referenced in your code. This kind of .jar file is mostly useful for sending to other users who have eclipse so that they can upload your project. So, you will need something different if you want to include referenced libraries in your .jar file. One way of doing this is setting the classpath of these libraries in the manifest file. This can be tricky, because you need to have the absolute location, which typically won’t be the same if you try sending this application to someone other than yourself. Instead, what I did was created a .jar file that physically included the libraries in it.
I don’t know of a way to do this with eclipse’s default .jar file creation. Instead, I downloaded a plugin for eclipse called “FatJar”. You can find that plugin here. In eclipse, click on Help->Install new software. You should see this window:
Click on “Add…” and specify the location of the plugin. It’s listed on that website as the “url Update Manager”. Set the name of the plugin to “FatJar” and click OK. After a while, you should see the plugin appear in the large text field. Select it and click on Finish.
Now, when you right click on a project and select “Export”, you should see a new option that says “Runnable JAR file”. That’s the one you want. Select it and a slightly different looking window will pop up:
The “Launch Configuration” is the location of your main() method. The “Export Destination” is where you want to send the jar file. As far as library handling goes, I’ve only ever tried the first option, “Extract required libraries into generated JAR”. That works for me, but I’m not sure what the difference is between that and “Package required libraries into generated JAR”. Feel free to experiment if you feel bold. Once you have that all selected, just click Finish. You’ll notice that you don’t get asked about manifest files or .jardesc files, which makes sense since this is just a plugin. So now, if you did everything right, you should have a .jar file that you can run just like any other application. Depending on the size of your libraries, the .jar file may be very large. Now you’re including the libraries in the .jar file as opposed to just pointing to their location.
Now there are ways to create .jar files without using eclipse. I just find it much easier to use eclipse to do this since it has all the files you are working with. However, if using eclipse is not an option for you, you can check out the Java Tutorials website to see a good tutorial of how to create a .jar file using unix commands. Again, I think the trickiest part of this comes from creating the manifest file and dealing with your referenced libraries (either setting the classpath or including them physically). I have created a jar file outside of eclipse before, but I have not been able to get one working properly with it’s libraries without using the eclipse method. If anyone has any success with that, they may want to share it with the group.