[LWJGL] How to properly load images and use them in a JAR(SOLVED)

Figured it out :smiley:

What does cmd exactly say?

It tells me:

java.io.FileNotFoundException: res\FlubberFlap.png (The system cannot find the p
ath specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:131)
        at FSMain.MainFS.main(MainFS.java:68)

In line 68 I use:

wood = TextureLoader.getTexture("PNG", new FileInputStream(
					new File("res/Baddy.png")));

Someone said not to use a new file, but im not sure exactly what to do. If you can look at my source, my main class is MainFS.

Remember, if it’s in a Jar it’s not typically accessible through the strema. You have to do somethin’ extra to get it. Try (And replace THISCLASS with the class making use of that command):


TextureLoader.getTexture("PNG", THISCLASS.getClass().getResourceAsStream(new File("res/Baddy.png"));

That’s also another problem, I get this error in that syntax:

Cannot make a static reference to the non-static method getClass() from the type Object

.
Maybe because I run everything in my main method. The main class doesn’t have a constructor.

Well then reference the class itself.


TextureLoader.getTexture("PNG", ThisClass.class.getResourceAsStream(new File("res/Baddy.png"));

I tried

ef = new File("res/Baddy.png");
wood = TextureLoader.getTexture("PNG", MainFS.class.getResourceAsStream(ef.getPath()));code] and got the exception :

Exception in thread “main” java.lang.NullPointerException
at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:64)
at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:24)
at FSMain.MainFS.main(MainFS.java:75)




I also tried

TextureLoader.getTexture(“PNG”, ThisClass.class.getResourceAsStream(new File(“res/Baddy.png”)));



and got the exception: 

The method getResourceAsStream(String) in the type Class is not applicable for the arguments (File)

  :emo:

Er, remove the “new File” part. That was my mistake!


TextureLoader.getTexture("PNG", ThisClass.class.getResourceAsStream("res/Baddy.png"));

			wood = TextureLoader.getTexture("PNG", MainFS.class.getResourceAsStream("res/Baddy.png"));

gives the exception :

Exception in thread "main" java.lang.NullPointerException
	at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:64)
	at org.newdawn.slick.opengl.TextureLoader.getTexture(TextureLoader.java:24)
	at FSMain.MainFS.main(MainFS.java:75)

Only other thing I can think of is that the file isn’t being loaded correctly some way.

Try: The getResouceAsStream and then print out its type. It might be that you’re not getting anything because the file isn’t showing up in the .jar.

Aren’t you forgetting the “/” in front of res?

My res/ is a resource folder that’d not under the src/.


Ignore the error in Begginer. Is this right? I have a video loading of what I did. Ill post it in a second

Here is the video of what I did: http://www.mediafire.com/watch/vjsnf1b1xoy6kdz/texturesssss.mp4

Wouldn’t that cause problems on Unix-like systems where a path starting with / is treated as absolute?

From the docs: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

[quote]If the name begins with a ‘/’ (’\u002f’), then the absolute name of the resource is the portion of the name following the ‘/’.
Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with ‘/’ substituted for ‘.’ (’\u002e’).
[/quote]

Here are some Image Utilities I use all the time: http://pastebin.com/JBQ8B8Y0

To load a image call:


wood = ImageUtils.loadBufferedImage("res/Baddy.png");

This will work for all Systems, for eg, Windows, Linux, Mac.

You don’t always need to use the class like this:


MainClass.class.getResourceAsStream("res/resource.png");

You could just do this:


"res/resource.png"

And then put a res folder in the same folder with the jar. This is a really cool thing to do when you want your players to be able to edit your textures like texture packs in Minecraft. I believe thats how Notch did that anyways.

Given the topic title, I’m assuming that OP wants the resources in the jar.
Also, doing so doesn’t stop anyone who wants to from modifying resources.

I specifically said:

[quote]This is a really cool thing to do when you want your players to be able to edit your textures like texture packs in Minecraft.
[/quote]
So yes, I know that people can edit it. And thats kinda what you wanted them to do. Well, if you wanted texture-packs anyways.

Wouldn’t I need to put it in a zip? I want them to just download the game and run it without any other files.

What is OP?