Solution to problem running SWT app from Jar

So I was writing a small application for a job I had and decided to through in a nice little interface using the SWT kit, since I wanted a little experience with this. The problem came about when I tried to create a JAR out of it, apparently there were a tonne of dependency issues with SWT that complicated the process of packaging the application. I did find some help though, which I’ll duplicate below along with the referencing URL, because in truth, this was the only walkthrough of many tried that worked for me. Enjoy!
Java Forums – problem running SWT app from Jar
To Create an Executable SWT JAR…

1. Make sure the SWT jar file is included in the Eclipse project build path
a. Download the standalone SWT jar file from eclipse.org even if you already have the ones that come with Eclipse. It is called “swt.jar” and you will package it with the executable JAR. Make sure it is the same version of SWT that you used to make your program.
b. In Eclipse, import swt.jar via Project Properties::Java Build Path::Libraries::Add External JARs. (Make sure to include the source .zip file). Also make sure to select the swt package in the “Order and Export” tab.
c. At this point, you should be able to compile and run your SWT application in the Eclipse SDK.

2. Do a standard Eclipse jar file export
a. Right click on the main .java project file and select “Export…”.
b. Choose “JAR File” from the list.
c. Select the relevant packages and .classpath and .project resource files
d. Make sure “Export generated class files and resources” is selected and browse to the location where you want the JAR file to be created.
e. Specify “Generate the manifest file” and don’t seal the JAR or any packages. Choose the class containing the main method as the “Main class”.

3. Change the manifest file
a. Navigate to the JAR file you created and open it with WinZip.
b. Open the MANIFEST.MF file inside and copy the contents.
c. Create a new file called “MANIFEST.MF” and paste in the contents of the old manifest file.
d. Add the line “Class-Path: ”. For simplicity, you can just keep a copy of the swt.jar file in the same folder as the executable jar, in which case, the line is “Class-Path: swt.jar”. Make sure there is a carriage return after the last line in the manifest file, or that line will not be parsed.

4. Replace the manifest file
a. Make sure the manifest file and the executable jar are in the same folder.
b. Open the command prompt and navigate to the containing folder.
c. Enter the command “jar umf MANIFEST.MF ” to update the manifest file with the class path information.

5. Package the JAR with SWT files
a. Put the updated executable JAR file, swt.jar, and the associate DLL in the same folder. The DLL can be pulled out of swt.jar and should have a name like “swt-win32-####.dll” on Windows.
b. The JAR file should now successfully execute. On Windows, you can usually just double click the JAR file in explorer and it will open. If that does not work, the command “java –jar ” on the command line has the same effect.

5 comments

  1. iris Nov 10

    Hi
    I followd what u have mentioned step by step .. but without any use .. i am using Eclipse IDE 3.1.1? so any hints?

    Thanks
    Iris

  2. Shadi Dec 25

    Thank You so much , you saved me 2 month of hard working from beeing a time waste.

    I followd you’r instruction , although i had to guess in some cases what you have meant , since the instruction hasent been all put in right order.

    Thanks,
    Shadi

  3. JS Apr 25

    Hi Redditech,

    It works great I followed your instructions all the way except 3) Change Manifest file and 4) Replace Manifest file. You are absolutely right that we have to change and replace the manifest file so that it includes the “class-path: swt.jar”. The way I did that:

    a) Open Eclipse and Create a File named MANIFEST.MF
    b) copy all the content in the old MANIFEST.MF and add the line “class-path: swt.jar” and hit carriage return. I tried at first by creating a notepad but it doesnt work since it has a .txt extension. You have to use eclipse and create new file. Save the newly created file as MANIFEST.MF
    c) Navigate to the JAR file you created from previos step (see Redditech description above) and open it with WinRAR. Then I just drag and drop the new MANIFEST.MF into the folder and it automatically replaced the old MANIFEST.MF

    Follow step 5 as outlined by Redditech. It works very well.

    Thank you!
    js

  4. Nic Soltani Sep 10

    I always had the right manifest files. Problem seemed to be in including swt src files in the build path.
    It worked, cheers

  5. mullllen (czech) Sep 24

    gorgeous, lots of browsing and finally I found this, great JOB. but just for my environment:

    I tried to execute command “jar umf MANIFEST.MF” but “jar” command wasn’t recognized as an command so that’s where I ended, well but including the “Class-Path: swt.jar” line into the MANIFEST was enough and it works well, thanx again to the author |o|

Leave a reply