开发者

Eclipse create java executable with external libraries

开发者 https://www.devze.com 2023-04-09 12:51 出处:网络
I\'ve seen this topic in this forum but it I need a more basic explanation on how to do this. I\'ve done a program in Java with some external libraries (LWJGL and Slick).

I've seen this topic in this forum but it I need a more basic explanation on how to do this.

I've done a program in Java with some external libraries (LWJGL and Slick).

So this is what I've done and my program won't start anyway, tell me where I've done wrong.

I have Eclipse 3.7.1

  1. My project is opened in Eclipse and runs well in Eclipse
  2. I click File -> Export
  3. I select Java -> Runnable JAR file
  4. Here I don't know what to choose in Launch configuration, when I click the dropdown I get the option to choose my main class so I do that.
  5. I select an export destination
  6. I select the option "Package required lib开发者_开发百科raries into generated JAR" under Library Handling
  7. I don't know what ANT script is so I don't use that
  8. I click Finish
  9. I copy my images-folder to the same location as the generate JAR-file
  10. I try to start the JAR-file, something loads in the background but nothing happens, no window shows up, nothing.
  11. I check the Task manager in windows and sees that a javaw.exe is running

What did I miss?

My program uses images for graphics like this:

image = new Image("images/filname.png");

I wonder if I need to change the paths before exporting or the method to load these?

Thanks!


If you want to get things from inside a jar file you need to have them in your classpath and access them as resources.

The constructor you use, refers to a physical file which cannot peek inside a jar-file.


(EDIT) Also note that you have no guarantee where the current working directory is. Hence any relative references may break, as you see.


Read this to learn how to use JarSplice to export your Eclipse project to a runnable jar-file. Regarding images, you can put them in the same directory as a class file (in the jar) and then write

SampleClass.class.getResourceAsStream("image.png")
to retrieve an InputStream of the image. Then you can load it however you like.


I had the same problem and I was able to fix it. All I did was copy the data folder (which contains my resources) into the *.jar file. You can do this for example with WinRAR.

0

精彩评论

暂无评论...
验证码 换一张
取 消