Custom files in a jar?

Wasnt sure where to put this, i couldnt find a programming help section… so since the games 2d i put it here

Okay, a wrote a little test game in java (screenshot here http://stars.hybd.net/games/ruby/ruby4.jpg ) The thing is it uses externally written .map files to load the maps, and when i finished the test and packaged it in the .jar file it didnt work.

I tried quite a few methods of loading the .map file, packaging it in the .jar, using a URLConnection to the harddrive , and just loading it off the hard drive but non seemed to work (im fairly positve its the opening of the file thats not working, as im using code that prevously worked in .jar format). Also the java.sun.com tutorial didnt really cover this (i looked over it, and it had some jar api stuff, but im not sure if thats what i want)

Now i looked around sun’s docs, and i think i read somewhere that i need to sign the .jar in such a way that i get full access to the hard drive (im guessing Java has security restrictions on this), so maybe thats why its not loading… can anyone clarify this for me? (i only need read access btw)

And for the actualy question, is there a way i can just place the .map file in the jar and load it from there? And if so, can anyone point me to an example, perhaps loading a .txt file from withing a jar?

Yet another self plug, I do apologise. If you take a look at this tutorial:

http://www.cokeandcode.com/info/webstart-howto.html

At the bottom there is a section of retrieving resources from jar files. The code snippet is this:


// Getting hold of a URL to sprite
ThisClass.class.getClassLoader().getResource("sprites/mySprite.png");

// Getting hold of a stream to a sound
ThisClass.class.getClassLoader().getResourceAsStream("sounds/bang.wav");

“ThisClass” being the current class. However, I normally wrap this up in a single class that handles retrieving all resources for me.

I have a feeling the JGF page will soon release some source snippets around this area too.

Kev

PS. You don’t need to sign the jar unless you’re distributing it to a controlled environment (like webstart).

Reading a textfile from the same jar:


try
{
      BufferedReader in=new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/stuff/bla.txt")));
      String line="";
      int i=0;
      while ((line = in.readLine()) != null)
      {
            StringTokenizer st=new StringTokenizer(line);
            [...]
            i++;
      }
}
catch(IOException ioe){ioe.printStackTrace();}

It isn’t much different.

Okay, it worked, thanks a lot for your help. I always thought that those commands were only to be used for files java natively supports like .pngs and it’s soundformat but i was wrong.

Thanks again.