wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how to load a png-file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to load a png-file?" Watch "how to load a png-file?" New topic
Author

how to load a png-file?

Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 530
    
    8
hi all,

First of all, I'm sorry if my problem has been asked many times before. I can't find any specific questions about this. But here goes...

I have just finished one of my very first java programs. I use the NetBeans IDE, and I stumbled on the following problem.

My source files are in a map called "Spirograph", and I use the package "Spirograph" in my java files. One of these files needs to load a file called "blob.png", which is in the same map, for simplicity.

I first tried to use the following code to load this png-file:



but that didn't work; the URL was a null pointer. The code above I picked up from some book, but I actually don't understand a thing about class loaders.

I then tried the following code:


and when I compile and run this program from Netbeans it actually works! But this hard coding of the path is very unsound of course, and running the jar-file (also from Netbeans) fails (I don't know the reason, for the dos-window that appears disappears too soon to read what's up).

So the question is: how can I locate the place of my package?

Thanks in advance!

Piet
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41587
    
  54
Welcome to JavaRanch.

The crucial question is: where is the file located? If it's inside the jar file, then using the classloader approach is the only possible way. If it's outside the jar file, but in the same directory, then "new File("BLOB.PNG")" should work. Either way, be careful about capitalization - on some operating systems "BLOB.PNG" is not the same as "blog.png".

If you run the jar file outside of the IDE you'll get to see any error messages. Open a command line window and type "java -jar MyJar.jar"; then any output is shown will stay visible.


Ping & DNS - my free Android networking tools app
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 530
    
    8
hi Ulf,

thanks for the welcome and the reply.

First of all, how can one check what's in a .jar file?

The idea is indeed to run the program as a jar file. But the classloader approach still doesn't work Running the jar from a command window, I keep getting the error of the null pointer. All java source files and the png file are in the same map "Spirograph". All java source files start with "package Spirograph". Using the approach of the File f = <hard coded path> gives a working jar file, but of course, that makes it only working om my machine.

So I'm still struck. Any idea where I can find more information about using packages and resource files?

Greetings,
Piet

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41587
    
  54
First of all, how can one check what's in a .jar file?

The jar tool can do that. "jar tf MyJarFile.jar" will list its contents. But unless you specifically included the image in the jar file when it got created, then it won't be there

All java source files and the png file are in the same map "Spirograph".

I don't understand what you mean by this - what is a "map" in this context? The source files are irrelevant at runtime; only the class files matter.

Using the approach of the File f = <hard coded path> gives a working jar file, but of course, that makes it only working om my machine.

Have you tried removing all the directories from the path, like I suggested with "new File("blog.png")"? That would work on all machines. But if the point is to distribute the image file along with the class files (as part of the jar), then you'll need to use the resource approach. Something like

InputStream in = getClass().getResourceAsStream("images/blob.png");

will look for an "images" directory relative to the current class in the jar file. If you want a top-level "images" directory, use

InputStream in = getClass().getResourceAsStream("/images/blob.png");
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 530
    
    8
hi Ulf,

The jar tool can do that. "jar tf MyJarFile.jar" will list its contents. But unless you specifically included the image in the jar file when it got created, then it won't be there


I tried this; "blob.png" is in the jar file.

I don't understand what you mean by this - what is a "map" in this context?


By "map" I mean folder or directory.

Have you tried removing all the directories from the path, like I suggested with "new File("blog.png")"?


Yes I have, I used the following code:



Running this code, I get the following result:

"f is ongelijk nul! (means "f is unequal nul")
IO Exception helaas! javax.imageio.IIOException: Can't read input file!
"

Trying the following code:



Gives me this result:

"sun.misc.Launcher$AppClassLoader@11b86e7
Exception in thread "main" java.lang.NullPointerException
"
(because imageURL = null, the .toString method fails)

Finally, as I wrote in my opening post, the following code works:


File f = new File("C:/Documents and Settings/Piet/My Documents/Java Programma's/Spiro2/src/Spiro2/blob.png");
if (f != null) {
System.out.println("f is ongelijk nul!");
sprite = ImageIO.read(f);
}
else {
throw new IllegalArgumentException("f is null!!!");
}


Well, I've experimented the whole afternoon now, and it seems I have solved the problem in NetBeans. The directory structure made by Netbeans was:

Spiro2/src/spiro2

In this folder were all my java files and this png file. Every java file started with "package spiro2;". However, issuing the command "File f = new File("blob.png")" makes Java look into the folder "src" and NOT in the folder "spiro2"! So, by altering the code to this:



it all works. Phew!

Anyway, thanks for all your advices, and I hope that other beginners may benefit from my hard learned experience.

Greetings,Piet
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

This is just me nitpicking but FWIW you can simplify your code just a tad. As long as you aren't doing this from a static method:



Can become:



And if you don't need a reference to imageURL anywhere else



GenRocket - Experts at Building Test Data
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to load a png-file?