ghigareda
ghigareda

Reputation: 21

Creating an executable jar using Eclipse

It export it fine as a Jar, but when I double click the project it won't load up.

I'm building a Java Slick2d game (my first). I've tried on both windows 7, and Ubuntu 12.10. Any suggestions?

Upvotes: 2

Views: 5427

Answers (3)

Zach Latta
Zach Latta

Reputation: 3331

If you run the exported jar file from the terminal, you'll see an Unsatisfied Link Error. To resolve this, you need to include the LWJGL natives. If you want everything in a self-contained jar, the easiest way is by using JarSplice.

Let's walk through how to do this step-by-step.

  1. Open up JarSplice

    Opening Screen

  2. Add the jar you exported from Eclipse.

    Adding exported jar

  3. Add the LWJGL natives. These can be downloaded from http://www.lwjgl.org/download.php. I'll be exporting for Linux, so I'm going to include the Linux native files. The native files should be in a folder called native.

    Adding natives

  4. Now specify the main class in your project. Don't forget to include the class's package.

    Specifying main class

  5. Click Create Fat Jar and you're done! You can also export to a Linux .sh, Mac .app, or Windows .exe. Just make sure you include the appropriate natives.

    Creating fat jar

  6. Have fun sharing your game with others!

    Game!

Upvotes: 1

Jim Jiang
Jim Jiang

Reputation: 21

Find file "MANIFEST" in the jar file, and add line : "Main-Class: {Your executable class name (the class with main method)}"

Upvotes: 0

TheWhiteRabbit
TheWhiteRabbit

Reputation: 15758

make sure you mention the Main Class: in META-INF

Detailed instructions

The detailed instructions for creating an executable JAR file for a stand-alone SWT file are listed below.

  1. Create a runtime folder for the desired runtime target on your system (e.g., c:\swt\runtime-linux). Note that the target platform does not need to be the same as your development platform.

  2. Find the correct SWT JAR file for the desired target platform. You can download the desired ZIP file from the SWT website. For example, for Eclipse 3.3 and a target platform of Linux, download the file swt-3.3.1.1-gtk-linux-x86.zip. Expand this ZIP file and copy the swt.jar file to the runtime folder. Remember that this swt.jar file is specific to one platform, in this case Linux.

    3.Create a manifest file for your application using the Eclipse text editor (e.g., myapplication-manifest.txt). The text of the manifest should be as follows:

    Manifest-Version: 1.0

    Class-Path: swt.jar

    Main-Class: mypackage.MyClassWithMainMethod

    (blank line at end of file)

4.Make sure the manifest file ends with a blank line. Put the name of your package and class that contains the main() method for the Main-Class. In Eclipse, select File/Export/Java/Jar file and press Next.

  1. On the JAR File Specification dialog, select the source files for the classes you want in the application. In the export destination, browse to the runtime folder and enter in the desired name of the JAR file (e.g., myapplication.jar or myapplication_linux.jar). Press Next.

  2. On the JAR Packaging Options dialog, make sure the "Export class files with compile warnings" box is checked. Otherwise, if your source files have any compile warnings, they will not be included in the JAR file. Press Next.

  3. In the JAR Export dialog, select the option "Use existing manifest from workspace". Browse to the manifest file you created above. Press Finish.

  4. If the JAR file already exists, you will be asked to overwrite it. Select Yes. If your project had any compile warnings, a message will display. If so, press OK. At this point, the JAR file for your application has been created in the runtime directory. If needed (i.e., your target platform is different than your development platform), copy the runtime directory to a directory on your target platform.

  5. In your operating system's file explorer, browse to the runtime directory and run your JAR file. For example, in Windows, you can just double-click on it in the Windows File Explorer or, from the "cmd" prompt, you can enter the command: java -jar myapplication.jar. The application should run.

Upvotes: 0

Related Questions