1 2 Previous Next 18 Replies Latest reply: Jul 27, 2012 9:39 AM by 952145 RSS

    imageio read returns null

    952145
      I have a program that reads an image from inside a jar (buillt dynamically in code) using ImageIO.read(URL);

      This works fine and I can read the image as many times as I like. However, if I rebuild the jar in code and try to read the image of the same name a second time the read returns null without throwing any Exceptions.

      It's as if java has cached the stream to the original file even though I have created a new URL.

      Any ideas anyone?
        • 1. Re: imageio read returns null
          sabre150
          949142 wrote:
          Any ideas anyone?
          Maybe the URL you have created is not valid or the new jar does not contain the image but since you have not posted it or an index of the new jar file it is almost impossible for anyone here to help.

          P.S. To create an index: jar -tf new-jar.jar
          • 2. Re: imageio read returns null
            952145
            The url is fine and the file still exists - I have checked both of these. Before trying to read the image I check the file exists which it does.

            Remember everything is OK until the jar is rebuilt - it's this process which causes the read to then fail.
            • 3. Re: imageio read returns null
              sabre150
              949142 wrote:
              The url is fine and the file still exists - I have checked both of these. Before trying to read the image I check the file exists which it does.

              Remember everything is OK until the jar is rebuilt - it's this process which causes the read to then fail.
              Something in your re-build process failed or it would still work but we have no view of anything you have done. I still say the jar or the URL is flawed. Until you post the URL and the index it is impossible to say anything more!
              • 4. Re: imageio read returns null
                952145
                This is the toString() of the url

                jar:file:/C:/Users/Neil/Oscar/GAR3_0000000002_C1.ddj!/temp/model_1/model_thumbnail.jpg

                And here is the index of the jar file

                temp/application.xml
                temp/execution_list
                temp/notes.xml
                temp/preferences.inp
                temp/survey_data.xml
                temp/template_1
                temp/template_10
                temp/template_11
                temp/template_12
                temp/template_13
                temp/template_14
                temp/template_15
                temp/template_2
                temp/template_3
                temp/template_4
                temp/template_5
                temp/template_6
                temp/template_7
                temp/template_8
                temp/template_9
                temp/history/history.xml
                temp/model_1/autofit_settings.xml
                temp/model_1/model_image.jpg
                temp/model_1/model_thumbnail.jpg
                temp/model_1/preferences.inp
                temp/reports/conservatory_roof_fabrication_report_001.pdf
                temp/reports/survey_report_001.pdf
                temp/saved_materials/dpc_1.png
                temp/saved_materials/ext_paint_work_5.png
                temp/saved_materials/ext_wall_0.png
                temp/saved_materials/flooring_3.png
                temp/saved_materials/int_skim_4.png
                temp/saved_materials/int_wall_2.png
                temp/saved_materials/paving_8.png
                temp/saved_materials/saved_materials.xml
                temp/saved_materials/skirting_6.png
                temp/saved_materials/stone_9.png
                temp/saved_materials/window_ledge_7.png
                • 5. Re: imageio read returns null
                  sabre150
                  The image is indeed in the jar file but since the jar file contains no class file entries I have to assume you have a main jar file somewhere that uses this URL. What was the original URL and how is this URL generated then used in your code?
                  • 6. Re: imageio read returns null
                    952145
                    Here is the code:

                    {
                    try {
                    URL url = new URL("jar:file:/C:/Users/Neil/Oscar/GAR3_0000000002_C1.ddj!/temp/model_1/model_thumbnail.jpg");
                    image = ImageIO.read( url );
                    }
                    catch ( Exception e ) {
                    System.out.println(" VSManager.getConservatoryImage : Failed to load image : " + im_res.toString() );
                    }
                    }
                    • 7. Re: imageio read returns null
                      sabre150
                      So your new jar file is named GAR3_0000000002_C1.ddj and is located in C:\\Users\Neil\Oscar ? What was the original URL?

                      I'm a little concerned that this is an absolute explicit URL since it implies that the jar file is in a particular location. It is normal that URLs within jar files are computed from class path information so that they do not depend on the absolute location of the jars. I'm also concerned that you may be trying to modify a commercial program without getting permission of the copyright holders.
                      • 8. Re: imageio read returns null
                        952145
                        I think you are going off on a tangent here. I have hard coded the URL to make it easy to read the code. There is no class in the jar because the jar is a compressed save of a 'job' from our main program and is data used for reopening the job. The commercial program is our own. Can we get back to the issue of:

                        1. The image read works OK to begin with.
                        2. The jar is rebuilt.
                        3. The image no longer reads in.
                        • 9. Re: imageio read returns null
                          sabre150
                          949142 wrote:
                          I think you are going off on a tangent here.
                          Your opinion not mine.
                          I have hard coded the URL to make it easy to read the code.
                          So in the original version the URL was generated in a different manner. As I previously asked, may we know how?
                          There is no class in the jar because the jar is a compressed save of a 'job' from our main program and is data used for reopening the job.
                          I never commented on the lack of classes in the jar!
                          The commercial program is our own.
                          So why not ask the program developers what you are doing wrong?
                          Can we get back to the issue of:

                          1. The image read works OK to begin with.
                          2. The jar is rebuilt.
                          You also re-package the image file and you change the way the URL is generated so you must also change the Java code. i.e. it is not just a simple matter of re-building the jar containing the image you must also re-jar the application.
                          3. The image no longer reads in.
                          You still have not provided the original URL or indicated whether my assumption about the name and location of the jar file are correct. You have only shown the code that you think is causing the problem ( since you say the returned image reference is null you are probably right ) but since I don't really understand why you are making this change and don't know what else you are changing I am working blind.

                          I have no idea why you are making this change and why you don't just ask your developers.

                          Sorry but I can't help. You must wait for someone else to take up the challenge.
                          • 10. Re: imageio read returns null
                            EJP
                            What did the JAR file look like before you rebuilt it? And what else changed at the same time?
                            • 11. Re: imageio read returns null
                              952145
                              The jar file is identical in structure just the contents of some of the files have changed. So the original and new url are identical in that they point to the same image file in a jar of the same name.

                              I'm sure it's because java is caching something somewhere to optimise the connection into the jar?
                              • 12. Re: imageio read returns null
                                sabre150
                                949142 wrote:
                                I'm sure it's because java is caching something somewhere to optimise the connection into the jar?
                                Unless this is actually an Applet or a JNLP application then there is no associated cache unless your application created it.
                                • 13. Re: imageio read returns null
                                  952145
                                  It's a stand alone application - not an applet or JNLP.

                                  I'm only suggesting the caching because if you look in the URL.java source code there would appear to be routing through reusable URlStreamHandler classes to get the connection to the file to then open the read stream.
                                  • 14. Re: imageio read returns null
                                    EJP
                                    I'm not so sure about that. JarURLConnection might be caching. It would make a lot of sense for a remote JAR: in fact I'm quite certain that it does cache remote JARs, and that this is the underlying mechanism underlying class loading from remote JAR files, which certainly aren't read one class at a time.
                                    1 2 Previous Next