Domanda

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)
È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top