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
- My project is opened in Eclipse and runs well in Eclipse
- I click File -> Export
- I select Java -> Runnable JAR file
- 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.
- I select an export destination
- I select the option "Package required lib开发者_开发百科raries into generated JAR" under Library Handling
- I don't know what ANT script is so I don't use that
- I click Finish
- I copy my images-folder to the same location as the generate JAR-file
- I try to start the JAR-file, something loads in the background but nothing happens, no window shows up, nothing.
- 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.
精彩评论