We help IT Professionals succeed at work.

How do I read .png in jar file from url on same server as the applet I am trying to read the .png from?

rainglen
rainglen asked
on
302 Views
Last Modified: 2010-03-30
I am using the following code to attempt to load a .png file from a jar on the 'net:
  URL url = null;
  String ref = "/images/actors/29/boy_stand.png";
  url = new URL("jar:http://www.rainglen.com/testlaunch/readingpng_actors29.jar!" + ref);
  if (url == null)
  {
    fail("Can't find ref: " + ref);
  }
  // use ImageIO to read the image in
  BufferedImage sourceImage = ImageIO.read(url);

When I run from my IDE, the .png is loaded from the jar on the server and displays, etc, but when I run the same code on the server in applet or Java Web Start application form, my url var comes up null.
I'm assuming I don't need to sign my jars or set security to allpermissions in my .jnlp file since I am accessing the same server that my applet is on. I'm hoping this can be solved without signing jars, but I am willing to go that route if that is the only solution. By the way, I am able to write data to and from the server elsewhere in my code so is seems server access should be available in this case.  
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
You don't need to sign jars. Make sure the jar is in the archive tag for an applet and in resources for Web Start

String ref = "/images/actors/29/boy_stand.png";
BufferedImage sourceImage = ImageIO.read(getClass().getResourceAsStream(ref));
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
as i mentioned earlier you just need to include the image in your applets jars.
you can then access it the same way you access other images, eg

url = getClass().getResource(ref);
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> Java Web Start application form, my url var comes up null.

would not seem possible that it is null actually :)
check your java console for why its failing
CERTIFIED EXPERT
Top Expert 2016

Commented:
Your problem is that the URL is wrong. You can't 'read' the whole jar - you need to read an entry from it
CERTIFIED EXPERT
Top Expert 2016

Commented:
Ignore that last - missed the append

Author

Commented:
Thanks for you feedback. Let me clarify a couple of things. I am intentionally NOT including this jar in the applet archive tag or in in the resources section of the jnlp file, because it is my understanding that the jar would then get loaded during initial startup.

My intent here is to load certain resources from jars only if and when needed when the applet/app is running.

Again, I am attempting to load individual resources from jars on the server, my normal code for loading resources is already as follows:
            url = getClass().getResource(ref);
as you have suggested it should be in your commments.

I checked java console and the reason I'm suspecting that var url is getting set to null in the case of the applet/app attempting to create it, is because the fail line 'can't find ref' is appearing in my java console.
As this problem seems to occur when running as intended as an applet off the net or as JWS off the net, and does not occur when running from the IDE, I'm suspecting permissions/access problems. But I'm just guessing at this point.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Check the ref is correct
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
I can check the applet out too if you post the link

Author

Commented:
the link is www.rainglen.com/testlaunch/Launch.php, check your email for instructions

thanks much!

CERTIFIED EXPERT
Top Expert 2016

Commented:
Are you sure you're not still trying to load the images from outside the jar too?

Make sure you're not calling System.exit in the applet - it would need to signed to do that, but it's actually an irrelevant call in an applet anyway

Author

Commented:
'are you sure you're not still trying to load the images from outside the jar too?' I'm pretty sure because I have an if/else, complete code here:

      System.out.println("Ref to be gotten: " + ref);
//      fail("Ref to be gotten: " + ref);
     
      URL url = null;
      if (ref.contains("/actors/"))
      { //story images in the actors subdir are going to be loaded from the 'net
            int storyLevel = Rainglen.pupil.getStoryLevel();
            fail("contains /actors/");
            if (ref.contains(".png"))
            {
                  url = new URL("jar:http://www.rainglen.com/testlaunch/readingpng_actors" + storyLevel + ".jar!" + ref);
                  fail("contains .png");
            }
            else
            {
                  url = new URL("jar:http://www.rainglen.com/testlaunch/readingjpg_actors" + storyLevel + ".jar!" + ref);
                  fail("contains .jpg");
            }
      } else
      {
            url = getClass().getResource(ref);
      }

      if (url == null) {
        fail("Can't find ref: " + ref);
        return null;
      }

      // use ImageIO to read the image in
      sourceImage = ImageIO.read(url);

I am calling System.exit in the applet, so I'll take that out. Kind of wondering tho' do I call stop() instead?

Thanks for the help!
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>System.out.println("Ref to be gotten: " + ref);

Don't remember seeing that. Sure you've updated your applet?

>>Kind of wondering tho' do I call stop() instead?

To close the frame, just dispose
CERTIFIED EXPERT
Top Expert 2016

Commented:
The Swing way is to call

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

which is what you should probably do unless you've got special closing requirements when you need to handle it yourself
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> archive tag or in in the resources section of the jnlp file, because it is my understanding that the jar would then get loaded during initial startup.

not necessarily.
plus it will get cached, whereas as loading it from a url will requie the jar to get read everytime an image is reqyuested.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
sounds like what you should be doing is not jarring up the images at all, and just putting them in your apps classpath

Author

Commented:
OK objects, so what you're saying is put my images in www.rainglen.com/testlaunch where my codebase is? And if I give this a try, what would the code for loading them look like? Would it simply be this same line of code?

url = getClass().getResource(ref);

Will the applet look in www.rainglen.com/testlauch for the images?

Thanks for staying on this for so long, sorry I can't get back quicker but I teach 1st grade every day!

PS to CEHJ. I'm working on making sure my applet is updated on the net.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> if I give this a try, what would the code for loading them look like? Would it simply be this same line of code?

yes

Author

Commented:
Thanks for all the help. I managed to get the above code to work, it seems that it works as is, I just needed to update my launch dir with a completely new build and jar files, and it WORKED! my applet is reading images from the jar residing on the server with no problems. Thanks CEHJ for going the extra mile and actually running my applet and everything. I wish I had more points to give, you guys deserve it.

PS. I will probably post my applet exiting issue as a new questions, thanks for bringing it up.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I told you earlier it would work fine :) and that the problem was not with that line.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>I just needed to update my launch dir with a completely new build and jar files, and it WORKED!

That's why i said

>>Don't remember seeing that. Sure you've updated your applet?

;-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.