Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Really having trouble making jar files work with pictures

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Really having trouble making jar files work with pictures

    Quote Originally Posted by loui345 View Post
    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

    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.

    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(...).

  5. #5
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

  7. #7
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Really having trouble making jar files work with pictures

    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
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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()
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    loui345 (March 5th, 2013)

  12. #11
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Really having trouble making jar files work with pictures

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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.

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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.

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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

    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:
     System.out.println("cp="+System.getProperties().getProperty("java.class.path"));
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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?

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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:
    D:\Java\jdk1.6.0_29\bin\java.exe -cp .;acm.jar;..\.;objectdraw.jar;comm.jar;mail.jar TestCode12
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Having a trouble Making a program
    By ZeraX in forum Object Oriented Programming
    Replies: 7
    Last Post: December 9th, 2012, 04:07 PM
  2. [SOLVED] Having trouble making calls.
    By ntracing777 in forum Object Oriented Programming
    Replies: 9
    Last Post: June 9th, 2012, 01:05 PM
  3. [SOLVED] files size for files inside the JAR file
    By Natherul in forum Java Theory & Questions
    Replies: 3
    Last Post: February 9th, 2012, 03:17 PM
  4. Making Executable Jar Files.
    By Java Programmer in forum Java Code Snippets and Tutorials
    Replies: 4
    Last Post: January 19th, 2012, 12:14 AM
  5. How to create or edit .ser files in Jar files
    By xbill in forum Java IDEs
    Replies: 1
    Last Post: May 18th, 2011, 05:15 AM