2

How can i make an executable of my project in Java? I tried to right click on my project and selected export .

http://i.imgur.com/XvyDZ8i.png

http://img560.imageshack.us/img560/6379/3ey9.png

the problem is that the exported jar file wont open when I execute it! Have I missed something? And is there a way to make an .exe executable from my project?

when I execute the jar file in cmd it says :

Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.libr
ary.path
        at java.lang.ClassLoader.loadLibrary(Unknown Source)
        at java.lang.Runtime.loadLibrary0(Unknown Source)
        at java.lang.System.loadLibrary(Unknown Source)
        at org.lwjgl.Sys$1.run(Sys.java:73)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
        at org.lwjgl.Sys.loadLibrary(Sys.java:95)
        at org.lwjgl.Sys.<clinit>(Sys.java:112)
        at org.lwjgl.opengl.Display.<clinit>(Display.java:135)
        at org.newdawn.slick.AppGameContainer$1.run(AppGameContainer.java:39)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.newdawn.slick.AppGameContainer.<clinit>(AppGameContainer.java:36)
        at JavaGame.Game.main(Game.java:34)
Basilevs
  • 22,440
  • 15
  • 57
  • 102
youssef1700
  • 109
  • 11
  • 1
    1) Try running the jar file on the command line and check out any error messages that you might be getting. 2) Please search this and other sites regarding creating exe files from Java projects as this has been re-hashed thoroughly. You really want to avoid doing this in most situations, and your best bet is to work on creating a running Jar file. – Hovercraft Full Of Eels Jan 22 '14 at 22:40
  • Note that if you find that you are in the minority of situations where you absolutely need an exe, you **still** need to first have a running jar file. – Hovercraft Full Of Eels Jan 22 '14 at 22:43
  • possible duplicate of [How can I create a JAR/exe of a Java project?](http://stackoverflow.com/questions/2305762/how-can-i-create-a-jar-exe-of-a-java-project) – Hovercraft Full Of Eels Jan 22 '14 at 22:44
  • here is the error : http://i.imgur.com/5VIzPUU.png – youssef1700 Jan 22 '14 at 22:44
  • Can you post the error message text here? Not as an image but as *text*. – Hovercraft Full Of Eels Jan 22 '14 at 22:45
  • sorry it said too long text in the reply – youssef1700 Jan 22 '14 at 22:46
  • 2
    Not in comments, add it to your question (use **Edit** option) – PM 77-1 Jan 22 '14 at 22:46
  • @HovercraftFullOfEels this Eclipse question is not a duplicate of Netbeans one you provide a link to. Moreover here we have to deal with native libraries which are not even mentioned there. – Basilevs Jan 23 '14 at 02:48

2 Answers2

3

UnsatisfiedLinkError indicates that you're missing a native library (usually a .dll file on Windows, or .so file on Linux). You'll need to do one of the following:

  1. Not recommended: Copy the native library to a location on the default java.library.path (on Windows, this includes C:\Windows\system32)
  2. Not recommended: Copy the native library to a directory, then run your program with java -Djava.library.path=dir/containing/library -jar <jarfile>
  3. Recommended: Bundle the DLL in your jarfile, then modify your code to extract the DLL to a temporary directory and load it using System.load or System.loadLibrary.

You can use either of the first two solutions above as a quick hack to get it working, but neither of those solutions is very good. The best solution from a deployment standpoint is #3 above.

In your case, you're using the Lightweight Java Game Library, or lwjgl as referenced in your UnsatisfiedLinkError. So you'll need to include any DLL(s) that come with lwjgl.

When you unzip lwjgl, you'll notice that it has a native directory with a subdirectory for each supported platform. Here is a listing of lwjgl's Windows DLLs:

Windows DLLs in lwjgl zipfile

To implement solution #3 above and make your executable jarfile cross-platform:

  1. in your project/jarfile, create a separate directory for each platform
  2. put all the native libraries for each platform in the appropriate directory (it may be helpful to put them in the same directory as some utility class that you'll later use to extract them)
  3. when you export your program to a jarfile, make sure the native libraries are included
  4. look up the platform/operating system (e.g., System.getProperty("os.name"))
  5. in your Java code (probably in your main method or some utility method), create a temporary directory
  6. for whatever platform you looked up in step 3, extract the appropriate native libraries into the directory you created in step 4 (hint: use Class.getResourceAsStream to get an InputStream, then use Files.copy to extract it to a file)
  7. for each library you extract in step 5, call System.load("path/to/library_file")

See https://stackoverflow.com/a/1611367/44737 for a nice example including code.

Community
  • 1
  • 1
rob
  • 6,147
  • 2
  • 37
  • 56
  • what is the DLL file i need ? i'm using windows 8 – youssef1700 Jan 22 '14 at 23:12
  • 1
    Sorry, I should have been clearer. I'll update my answer. You need the DLL(s) that came with the Lightweight Java Game Library that you're using (lwjgl as referenced in the error). http://lwjgl.org/ – rob Jan 22 '14 at 23:18
  • yeah thx and i hope you can explain me how to do the recomended part, that mean where i have to use System.Load – youssef1700 Jan 22 '14 at 23:21
  • I've added some extra details, as well as a link to an example with code. – rob Jan 22 '14 at 23:39
  • the problem now is when i export my project into jar , the jar file doesn't contain the lib folder and the other folders containing the images for my project, i tried to archive them into the jar file manually compilation shows NullPointerException which mean that it doesnt find the files i've archived them ! – youssef1700 Jan 25 '14 at 00:45
  • 1. Double-check that when you're exporting the project to a jarfile, the library files are selected in the wizard. 2. Be sure the path you pass to Class.getResourceAsStream() is an absolute path within the jarfile--e.g., /lib/library.dll. If you still have trouble, please create a new question or edit this question and add your code and more details. – rob Jan 28 '14 at 21:58
2

You can either package everything in a jar manually using the jar command line tool or you can automate the process of packaging your jar using tools like Ant, Maven or Gradle.

Once you have your class files properly packaged in jar file, you can execute them by creating a script file appropriate for your operation system. For instance a batch file in windows or a bash file in Linux.

All you have to do is invoke the java command and provide your class path and the name of your application entry point.

#!/bin/bash
java -cp myApp.jar com.my.app.Main

And that's it. You execute your application by invoking that script.

Additionally, you can make arrangements to create an executable jar, by means of defining a MANIFEST file for you jar file. In that manifest file you can place a property Main-Class that indicates your application entry point and you can define another property called Class-Path which allows you to specify a list of other jars needed by your application.

See Running Jar-Packaged Software.

If you package your jar this way, some operating systems allows you to execute the application by simply double-clicking the jar.

But most probably, you still will need to create a script, just a bit different this time:

#!/bin/bash
java -jar myApp.jar

See the Java Tutorial: Packaging Applications in Jar Files.

Ultimately, if you really, really need to create an executable file, and if you're working on windows, you may consider a tool like WinRun4j.

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205