0

I have a problem extracting my current project into an executable jar file. When I'm in eclipse and press the run button everything works fine, but when I do

Export -> Runnable JAR File -> Choose right Project, Libary handling: Extract required libraries into generated JAR -> Finish 

and then start the .jar file; the only thing I get is a window with a grey canvas. It should start the game I programmed (spaceInvaders).

https://github.com/datosh/SpaceInvaders

Here is the link to the github where I uploaded the project. I really don't know where the error is and where to look. In my code? In my installed JRE/JDK? Am I doing something wrong while creating the jar?

Please help -.-

SeekingAlpha
  • 7,489
  • 12
  • 35
  • 44
datosh
  • 498
  • 7
  • 20
  • We don't need to see all of your code - but it would help if you could provide a *short but complete* program which demonstrates the problem. – Jon Skeet Jan 29 '14 at 20:55
  • (I'm pretty sure the problem is that you're trying to read the images via `new File(...)` rather than using `Class.getResource` though.) – Jon Skeet Jan 29 '14 at 20:56
  • 1
    Well the programm is relatively short, isn't it? Whole project is only 6 classes and < 1000 lines. You can import it and run it yourself. Why is using new File(...) a problem instead using Class.getResource? – datosh Jan 29 '14 at 20:59
  • Well you don't have an image file, do you? You've got a resource in a jar file. And no, I don't want to read through nearly 1000 lines of code to diagnose the problem. I'd rather read about 20, which is all it would take to diagnose it. As the person asking for help, you should be the one to put the effort into isolating the problem - and writing a good question which will be useful for others in the future. Please read http://tinyurl.com/so-hints – Jon Skeet Jan 29 '14 at 21:01
  • Well I have the image files. There are 4 images (.png) in the image folder. I really don't know how to shorten the program for you. Maybe you can throw me a hint and I will do my best :) – datosh Jan 29 '14 at 21:04
  • How to shorten the program: create a program which *just* loads an image. It doesn't even need to show it. When in doubt, consider what's really needed to demonstrate the problem - do you think all the aspects of moving, shooting etc are relevant here? – Jon Skeet Jan 29 '14 at 21:06
  • 1
    No I don't think so. I just provided the whole project so someone could import it and try to create a runnable. And without knowing that the problem might be the importing of the files/images I don't know what to test/exclude from the program. It could also have been the input handling, my exporting itself and so on... Anyway...give me a minute I try to strip it down. – datosh Jan 29 '14 at 21:11
  • If you included a portion of the console output that showed an exception, that would help. I posted one possible answer, if Jon Skeet is correct about the root cause. – E-Riz Jan 29 '14 at 21:12
  • I don't get any exceptions, just a gray canvas. Thats the part that confused me. If I had a starting point to look for the error I would have tried harder by myself. – datosh Jan 29 '14 at 21:22

1 Answers1

1

When your app is packages as a JAR, there are no files other than the JAR itself. The File class is only for accessing resources on the local file system, but, as Jon Skeet said, you need to read from the application's classpath when it's been packaged into a JAR. See https://stackoverflow.com/a/3376393/639520

Community
  • 1
  • 1
E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • Or just supply the ressources as they are in the project, right? I just tested to copy my images folder next to the .jar and the whole thing works. Thanks for explaining. Did not know that external ressources are not packed within the jar. And thanks for the link. Really helps to understand how this works! – datosh Jan 29 '14 at 21:18