Having Problem loading Image from URL

Hi,
I’m having problem loading Url images from jar file.
The image files are in the same package as the Class

By using this source loading resource in eclipse works fine:


for(int i=1;i<=6;i++){
URL url =CanvasResourceViewеr.class.getClassLoader().getResource("r"+i+".png");
this.resourceImages[i-1] = ImageIO.read(url);
}

But when i Export it in jar file i get an error:
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at client.framework.CanvasResourceViewer.(CanvasResourceViewer.jav
a:22)
at client.local.GameClient.(GameClient.java:65)
at client.local.Run.main(Run.java:25)
Any ideas ?

Sounds like the URL may be invalid?

What is the rest of the code? My guess is URL isnt pointing to the right domain/website/class or the resource inside isnt in the class’s root folder.

That or you’re loading one too many images? Try putting a System.out.println(i); in the forloop (Or just use break points and step through it) and see if it cycles through any of them or just blowups up the second it starts.

It works in Eclipse so URL should be valid…
I’m loading only 6 images so far , so they shouldn’t be too many.

Ah, sorry, I missed where you said only on export.

Is it possible that the .jar file itself is blocked on your firewall?

I’m loading local computer images placed in the class’s package.
How can i know if firewall blocks my .jar file? However I doubt.
I also noticed that IF i do wrong url in Eclipse i get the same error.

Are you sure that the CanvasResourceViewer is visible/in the root folder along with your exported jar (or inside the jar itself)?

Why are you using URL anyway? Will this file eventually be online? Why not just package the images in your jar?

EDIT: If you’re in windows, you can check the Windows Firewall. In Windows 7 the fastest way is start -> type “firewall” in the search bar -> “Allow a program through the firewall” and see if anything java related is on the list set to deny/private. I’ve never actually used URL, so I don’t know if there’s some wackiness like it has to “verify access” to the internet even before it can access anything, even a local file, and when it fails the check it just crashed.

I double checked about the class and its in the jar in the same place that images are.

Well i tried using simple file load but it again doesn’t work when i export into jar .
After my try with file load i tried to search at internet about my problems so i saw URL solution.

EDIT: Well im about 90% sure its not bs of Firewall

I doubt it’s the firewall too after I realized this was all being loaded locally. Just a random idea.

have you tried simply putting the images in /res/ and loading them like this or have you always been trying to pull them from somewhere else?:


BufferedImage img = null;
for(int i=1;i<=6;i++){
	try {
		img = ImageIO.read(new File("res/r"+i+".png"));
		this.resourceImages[i-1] = img;
	} catch (IOException e) {
		System.out.println("Oh god! I have failed you master! :("); //Your error message here. :P
	}
}

EDIT: After reading that code, eclipse might ask you to use a try/catch, just FYI, if so I changed the code as needed.

O, code is actually into try and catch.Thats how I got the error. Using File wont work since file in .jar are zipped.

Thats true. File won’t work internally in a jar.


O, code is actually into try and catch.Thats how I got the error.

What do you mean?

I think you need to post up a hierarchy of how your project looks and where your files are. Sounds like an issue with where your files are actually stored.

Here:http://postimg.org/image/byqwb6j43/

At exactly what index in your resourceImages array does the error occur?

That URL is a site. Do you use the sites URL or the actual image’s URL in the program?
It will probably not ignore the HTML and what-not.

1st index

@Drenius, a URL can be used to load a file from the local file system, which OP is attempting to do.

Yea, noticed that, sorry for posting unhelpful.

As I said the image is not is in site.
EDIT: Np.

OP, try loading like this:


URL url =CanvasResourceViewеr.class.getClassLoader().getResource("client/framework/r"+i+".png");

If I remember correctly you need to include the full path of the file relative to the root folder of the project.

Img: http://postimg.org/image/f2f9m78un/

Works !
Thank you, however I’m not sure why my code was working only with Eclipse.

Ask us again in another thread about that problem.