0

I'm trying to export a java project in eclipse as a runnable jar, but for some reason the runnable jar doesn't work. If I double click the executable jar, it doesn't do anything. I tried both extract and package required libraries into generated jar.

So I also tried to export some simpler projects, those worked fine. The biggest difference is my real project has files: images and xml files.

In code reference them like this:

File file = new File("Recources/test.xml");
ImageIcon imageIcon = new ImageIcon("Recources/" + num + ".gif");

The structure of the project looks like this:

enter image description here

But in the executable jar they look like this:

enter image description here

Thank you for your help.

Edit: I have tried the 'java -jar filename.jar', but now it says it can't find my resources folder, while in eclipse it can still find it.

Yadeses
  • 195
  • 1
  • 8
  • Have you tried running the jar from the command line using 'java -jar .jar'? – AlexPogue Oct 25 '15 at 15:37
  • How can I make sure it opens like that automatically without having to open cmd? – Yadeses Oct 25 '15 at 15:50
  • Could you please provide a log or something similar? It's anyone's guess as to what's going on without it. My guess is that the resource files are in a different directory when running from the development environment verses the .jar file. – Luke Melaia Oct 25 '15 at 15:53
  • What do you mean with a log? In the executable jar they are in the jar itself like in the second picture: file.jar -> com, meta-inf and all the recources. In eclipse: projekt -> recources -> all the recources. The error cmd shows is Java.io.FileNotFoundException: C:\Users\User\desktop\Resources\test.xml. This isn't weird because those are actually inside the jar, which is located at C:\Users\User\desktop\file.jar. – Yadeses Oct 25 '15 at 16:05

3 Answers3

1

Files in a JAR-File aren't just like files stored in your hard-disc. If you include files in a JAR, they'll be seen as a Stream of Bytes. So you have to use different methods to access these resources.

//To read/access your XML-File
BufferedReader read = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/test.xml")));
//To read/access your gif-Files
ImageIcon icon = new ImageIcon(this.getClass().getResource("/"+num+".gif"));

"/" is not the root-Folder of your file-system, but the root folder of the resources inside your JAR.

MikeVe
  • 1,062
  • 8
  • 13
0

The issue may be that Java is not the default program to run the jar.

Try right click -> Open with, and select the Java Runtime, and it should run successfully.

Make it the default program to enable double-click running.

Right click -> Properties -> Change -> C:\Program Files\Java\jre7\bin\javaw.exe

Inspired by stratwine's answer at https://stackoverflow.com/a/8511277

Community
  • 1
  • 1
AlexPogue
  • 757
  • 7
  • 14
0

So thank you all, but it seems like the problem wasn't the export only. There was an error I saw when I opened my program with cmd, I was using file name to open xml and images while I should have used inputStreams: https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html.

Yadeses
  • 195
  • 1
  • 8