-
Really having trouble making jar files work with pictures
Hello,
Quick question, I created a program for my small business. The program displays the image of the product i sell overseas. It works perfectly until I put it into a Jar file. See the problem is it will only display pictures before I create the Jar file. See the program is suppose to allow pictures to be added as you go.
Icon image = new ImageIcon(getClass().getResource(placeholder + ".png"));
that is the line of code I am using to add the pictures... The placeholder represents the name of the picture. If I drag the picture into the folder and compile the program it works perfectly. instead of compiling the program I make it into a Jar file and use it on other programs it does not work at all. What does not work is the picture does not display at all. Only time it works is if I drag the picture into netbeans and compile it like that. Does all pictures have to be embedded inside the Jar file for it to work properly? I thought I could add pictures as I go.
I can show more code, but I assumed this is a straight question so more code is not needed. If you need more code, let me know.
THanks in advance.
-
Re: Really having trouble making jar files work with pictures
It would be best if you showed us the contents of the jar file. You can do this from the command line easily by:
jar tf jar-file
where jar-file is the name of your file. Let's see exactly where your images reside (if they are even present at all) inside of the jar, relative to the class files.
-
Re: Really having trouble making jar files work with pictures
sorry, curmudgeon, I am n00b when you say "command line" do you mean the cmd prompt? My Jar file name is business.jar, so where would I type that in at?
Also, there should be no images associated with my jar file except the files that I used to decorate my Jframe with.
After the user enters the name of the file it gets stored into an excel sheet. I have my program retrieve the data and show a picture associated with the data. I use a combobox to select through the data. I hope this makes it simpler to understand.
-
Re: Really having trouble making jar files work with pictures
Quote:
Originally Posted by
loui345
sorry, curmudgeon, I am n00b when you say "command line" do you mean the cmd prompt? My Jar file name is business.jar, so where would I type that in at?
Yes, cmd prompt will work for Windows (but not for Linux).
I would use Windows Explorer to go to the directory that holds the jar file, open a cmd window from that subdirectory and type in,
jar tf business.jar
Quote:
Also, there should be no images associated with my jar file except the files that I used to decorate my Jframe with.
Hopefully the jar file will contain the images that you're having trouble displaying, for instance your placeholder + ".png" images.
Quote:
After the user enters the name of the file it gets stored into an excel sheet. I have my program retrieve the data and show a picture associated with the data. I use a combobox to select through the data. I hope this makes it simpler to understand.
Are you saying that the images are not held inside of the jar file? Please clarify this greatly since if this is true then you should not be using getResource(...).
-
Re: Really having trouble making jar files work with pictures
Hi, Curmudgeon,
The images are not held in the jar file. They're added as time goes. As an example, say I find a knew item i am looking to sell and I would like to add it to my already compiled jar file. I would like to be able to drop the picture in the directory and have it read it and display the picture. I hope this clarifies that.
If am I not suppose to use the getResource, what can I use in replace of it?
Thanks.
-
Re: Really having trouble making jar files work with pictures
If it is outside of the jar file, you can get it as a File or as a URL.
-
Re: Really having trouble making jar files work with pictures
Hi,
I attempted to do as you suggested still with no success. Before trying it, I thought it would def. work. Wondering why it is not working!!!
Here is the code I tried it's only two lines so do I have to use tags?
File f = new File("C:\\test\\"+placeholder+".png");
Icon image = new ImageIcon(getClass().getResource(f + ".png"));
I have also checked to see if the file has been found using the f.exists method. I located the file and it finds it but wont display the picture.
This is the path: C:\Test\phone.png.
I am really confused on why i am unable to display a picture using a path? When I drop the picture in the folder, it works perfectly. I know there has to be something i am overlooking. I greatly appreciate your insight in this matter.
-
Re: Really having trouble making jar files work with pictures
Quote:
File f = new File("C:\\test\\"+placeholder+".png");
Icon image = new ImageIcon(getClass().getResource(f + ".png"));
Does that code use the correct filename in the getResource() method?
Print out (f + ".png") to see
-
Re: Really having trouble making jar files work with pictures
Norm,
If I type out system.out.println(f), it shows the path with the extension of the file png, and I am also typing in the constructor the "png" could this cause a problem? if so, how do I fix it? The constructor for the getResource command requires a String to be present? I have even tried to remove the "png" from the constructor and still to no avail. Icon image = new ImageIcon(getClass().getResource(f + "")); So I am really stuck. This part is the most important because it is the meat of the program.
I am really baffled at why I cannot get this image to view.
I do appreciate you guys taking the time to help me out. I do indeed appreciate this service.
-
Re: Really having trouble making jar files work with pictures
The getResource() method uses the classpath to start the search for the file.
What is the full path to the file?
Where does the classpath point when the code is executed?
What path is given to the getResource() method?
The file needs to be at classpath + path given to getResource
D:\test\images\image.png
Where red is classpath and green is path in getResource()
-
Re: Really having trouble making jar files work with pictures
Hi Norm,
You nailed it. That was the problem I was having. I learned a lot from this. The program is working perfectly. I would not have figured that out without this community. I greatly appreciate it.
-
Re: Really having trouble making jar files work with pictures
-
Re: Really having trouble making jar files work with pictures
Man, I had it working perfectly,I moved a few things around, now it does not work, and I cannot make it work again. I thought I understood perfectly and as it turns out I do not.
I understand that using the File Class is the pathway. The GetResource Method looks into the directory. Look at my code please and let me know where I am going wrong with this process. Your explanation was crystal clear. It has to be something trivial with my code that I am just overlooking.
File f = new File("C:\\Test\\");
Icon image = new ImageIcon(getClass().getResource(f+"phone.png"));
My code seems straight and right to the point. Why is not locating the file? It is there I assure you of that. I also have a copy of the image in the main directory of the program. Once I remove the "f" from the constructor the picture appears on my Jframe. I cannot understand why it won't work.
Thanks.
-
Re: Really having trouble making jar files work with pictures
What is the classpath when you execute the code?
Where is the image file located?
-
Re: Really having trouble making jar files work with pictures
Hi Norm,
Thank you for working with me in this matter.
Okay, the path that I am using for my picture is: (C:\\Test\\"nameoffile.png"). Now, I know that this works as I had sucess following your advice. Like I said before, I moved a few things and now I cannot replicate it.
Icon image = new ImageIcon(getClass().getResource(f+"phone.png"));
thegetResource method should use the "f" to locate the path of the file, which is C:\\Test\\. The resource should then look inside the folder Test and find the image.png. Am I missing something with this process?
I am using this part for my class path:
File f = new File("C:\\Test\\");
then I use getResource Method to look inside the folder to actually find the image.
-
Re: Really having trouble making jar files work with pictures
The getResource() method does not take File object as an argument. Why are you using a File object as part of the argument?
getResource() uses the value of the classpath to find files. See post#10
What is the value of the classpath when the program executes?
-
Re: Really having trouble making jar files work with pictures
Hi Norm,
The reason why I was putting the File object into the argument is because I thought I was instructing the getResource method to look into the folder"C:\\Test". I assumed File object was a map and the getResource was the X on the map telling the program to look here. If my understanding is wrong, please correct it. If I am not suppose to put the object into getResource constructor, then how do I tell the getResource where to locate the image?
I am unsure on how to display the class path unless I use the File object.
-
Re: Really having trouble making jar files work with pictures
The path to the file is made of two parts:
the classpath
the path passed to the getResource() method.
See post#10
Quote:
how to display the class path unless I use the File object.
The File object is not related to the classpath.
This code will printout the classpath:
Code :
System.out.println("cp="+System.getProperties().getProperty("java.class.path"));
-
Re: Really having trouble making jar files work with pictures
Hi Norm,
Okay now it makes sense. Your last explanation clarifies a lot. I did not realize that it was in two parts. A classpath and class path. I was thinking it was all together. How do this?
I looked at post ten several times and I think I have an understanding of what you want me to understand. I just am having trouble applying to my given situation.
The code given worked. The results are:
cp=C:\Users\Jamal\Desktop\poi-3.8\poi-3.8-20120326.jar;C:\Users\Jamal\Documents\NetBeansProj ects\SaoriBusiness\build\classes
-
Re: Really having trouble making jar files work with pictures
To use getResource() the image file must be in a folder or path accessible from the classpath.
I don't see how to get from the classpaths you posted to the C:\Test\ folder.
You need to add a path to the classpath that points to C: or C:\Test
-
Re: Really having trouble making jar files work with pictures
Can you please point into a direction where I could learn to do this. I am glad that I know the problem. I am unsure on how to do this. In this situation, is getClassLoader method applicable?
-
Re: Really having trouble making jar files work with pictures
The classpath is set by using an option of the java command: -cp
For example:
java -cp .;C:\Test TheClassHere
sets the classpath to the current directory (.) and to C:\Test
Here is a sample of how I use the java command with -cp option:
Quote:
D:\Java\jdk1.6.0_29\bin\java.exe -cp .;acm.jar;..\.;objectdraw.jar;comm.jar;mail.jar TestCode12