Loading a static Texture inside a jar file

Hey guys!
I’m currently trying to export my game into a single jar file. I used JarSplice to create a Fat Jar.
My Game loads the Spritesheet as a Slick2D Texture in a static variable.

At first I tried to load the Texture like this:

public static Texture loadTexture(String path) {

		try {
			return TextureLoader.getTexture("PNG",new FileInputStream(path));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		
		return null;
	}

This worked in Eclipse, but not exported as a jar. I googled for a while and changed it to:

public static Texture loadTexture(String path) {

		try {
			return TextureLoader.getTexture("PNG",MyTextureLoader.class.getResourceAsStream(path));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		
		return null;
	}

Now the Game won’t even work in eclipse and I get a “Exception in thread “main” java.lang.ExceptionInInitializerError” when I try to run the game. More precisely when I try to bind the texture.

Could anyone help me?

I recently had the same problem and here is how I fixed it.

  1. Instead of putting all my resources in a res folder I put images directly in my texture package, sounds in my audio package, so on and so forth.

  1. I use davedes’s texture class (thank you davedes for the tutorials and all code has the proper copyrights attached).
	private static void initTexture(String fileName) {
		try {
			textures.add(new aufait.textures.Texture(Textures.class.getResource(("/aufait/textures/" + fileName))));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

Just as another example here is how I load my audio resources.

	private void generateSound(String fileName, int index, boolean lastSound) {
		BufferedInputStream bufferedInputStream = new BufferedInputStream(Audio.class.getResourceAsStream("/aufait/audio/" + fileName));
		WaveData waveFile = WaveData.create(bufferedInputStream);
		if (lastSound) {
			try {
				bufferedInputStream.close();
				waveFile.dispose();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		AL10.alBufferData(buffer.get(index), waveFile.format, waveFile.data, waveFile.samplerate);
	}

Thank you VERY much! It worked perfectly. I didn’t think this would be solved that quick :smiley: