Reading files in JARs from an applet
Basically, I've got this setup: A JAR files containing the normal class-files, a bunch of images and a text file that points to the images. When I run it as a frame (I've got two entry-points; one for applets and one for "normal" running), everything works as it should; it reads the text file from the JAR and loads the images, all fine. However, when I try to run it as an applet, it simply refuses to read the textfile unless it's outside of the JAR-file (at which point it reads the images inside the JAR without a problem). I've googled a bit and found a few pieces of code that apparently should fix the problem... but no such luck. Here's a few of the attempts I've made to read the file (the first and last works with the target file outside of the JAR, but not with it inside, and the middle just returns null, more or less as I expected it to):
I call it thusly (from another class):
public InputStream getInputStreamForFile(String path) throws MalformedURLException, IOException
// return getClass().getResourceAsStream(path);
// return ClassLoader.getSystemResource(path).openConnection().getInputStream();
return new URL(getCodeBase(), path).openConnection().getInputStream();
Here's another funky bit: If I change it to "/testfile.txt", it reads the textfile as it should, but it stops the (completely unrelated) image loading from working! Any suggestions?