1

I'm currently working on my own little video game in Java, but as of now when i give the Runnable Jar file to someone so they can play, i have to send them 22 other resource files which are required, and have them place it in the same folder. This is unbelievably inconvenient and i know that there is a way to automatically package these resources into the runnable jar file. If i cant do this then i would like to at least know how to access the files in the Runnable Jar so i could simply copy paste the files inside. I have combed the internet and no suggestions have worked. I can run the program properly in eclipse if i use this code:

    URL url = this.getClass().getResource("Jugger-Nog.jpg");
         image = ImageIO.read(new File(filePath));
         //image = ImageIO.read(url);

The first line creates a url, which is what i read on many web pages, but using that and the 3rd line of code did not work in the JAR or in Eclipse, but the second line on its own will work just fine in Eclipse. Any ideas, because the internet doesnt have many.

Colton
  • 55
  • 3
  • 11
  • you can use **URL url =Thread.currentThread().getContextClassLoader().getResource("Jugger-Nog.jpg");**, but make sure that image file should be on your class' root folder. – Asad Rasheed Jul 18 '11 at 06:40
  • Perhaps you'll find [this answer](http://stackoverflow.com/questions/7263505/how-to-package-resources-in-jar-properly "How to package resources in Jar properly") helpful? – oferei Jul 18 '13 at 08:57

4 Answers4

0

I had this problem too, but my solution was that the program checks if a folder in %app data%/roaming/ called for myApp exists. If it doesn't the program does a mkdir and makes that folder. Then it's just to let the program know the location of where all the source files is located. No need to hockey pokey around with your files.

Marko
  • 20,385
  • 13
  • 48
  • 64
Assaru
  • 1
  • 1
0

Setup Resource Folder

The first thing you need to do is setup a resource folder. In Eclipse, right-click on your project and navigate to New>Folder, call this folder whatever you want. Right-click on your project again and navigate to Properties. In this window click on Java Build Path and then on the Libraries tab. Click Add Class Folder and select the folder you created earlier. This is where you can put your resource files.

Accessing Your Resources

In your Java code you can use the following:

%CLASSNAME%.class.getResourceAsStream("/%FILENAME%");

Where %CLASSNAME% is the name of the class you are currently in and %FILENAME% is the name of the file you wish to access. This will return an InputStream. If you wish to load an image you can simply wrap it with a call to ImageIO.read(stream) for example:

ImageIO.read(%CLASSNAME%.class.getResourceAsStream("/%FILENAME%"));

Also, notice the slash the comes before the file you want to access, it is mandatory I'm afraid.

Why Can't We Use File

You can 't use a File because it represents an actual physical file on your file system, when you package it into a jar it is a part of your jar file and not an actual file on your file system.

Llew Vallis
  • 337
  • 4
  • 12
0

You should use:

InputStream in = this.getClass().getResourceAsStream("Jugger-Nog.jpg");
image = ImageIO.read(in);
Max
  • 2,917
  • 1
  • 16
  • 16
0

Is it so bad put the JAR file and the resource files just into the same directory, rather than package all of it inside the JAR?

You could package your game on an Installer which copies all of it to the Program's folder and puts some shortcuts.

everton
  • 7,579
  • 2
  • 29
  • 42