Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

How do I load a .png resource from a jar on the 'net?

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.buy_stand.png";
  if (specialcase)
  { //this image is going to be loaded from the 'net
        url = new URL("jar:http://www.rainglen.com/testlaunch/readingpng_actors29.jar!" + ref);
  } else
  {
        url = getClass().getResource(ref);
  }

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

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

The error I'm getting is:
javax.imageio.IIOException: Can't get input stream from URL!
      at javax.imageio.ImageIO.read(ImageIO.java:1369)
      at system.RGImageStore.getImage(RGImageStore.java:112)

The non-specialcase version works fine as it has for a couple of years.
0
rainglen
Asked:
rainglen
  • 10
  • 4
3 Solutions
 
objectsCommented:
the url is incorrect, should be http first. Though I'm not sure you can actually do it like that at all.
0
 
rainglenAuthor Commented:
Well, the syntax I'm using is derived from the link
http://java.sun.com/docs/books/tutorial/deployment/jar/jarclassloader.htm 
which has the following info:

>>>The JarURLConnection class and JAR URLs
>>>The getMainClassName method uses the JAR URL format specified by the java.net.JarURLConnection >>>class. The syntax for the URL of a JAR file is as in this example:

>>>    jar:http://www.xxx.yyy/jarfile.jar!/

>>>The terminating !/ separator indicates that the URL refers to an entire JAR file. Anything following the >>>separator refers to specific JAR-file contents, as in this example:

>>>    jar:http://www.xxx.yyy/jarfile.jar!/mypackage/myclass.class

>>>The first line in the getMainClassName method is:

>>>    URL u = new URL("jar", "", url + "!/");

>>>This statement constructs a new URL object representing a JAR URL, appending the !/ separator to >>>the URL that was used in creating the JarClassLoader instance.

Even if that part is right, I'm thinking I might still have a problem when the code gets to the line

 BufferedImage sourceImage = ImageIO.read(url);





0
 
rainglenAuthor Commented:
By the way, I am relying on this from CEHJ from an earlier question:

>>>You can load the files directly from the jar:

>>>URL url = new URL("jar:file:sounds.jar!/sounds/x.au");
>>>AudioInputStream audioStream = AudioSystem.getAudioInputStream(url);

which I assume means I can load from a jar located on my website as well, I'm basically just looking for the right syntax for it.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
>   String ref = "/images/actors/29.buy_stand.png";

u sure that image is in the jar?
0
 
Ajay-SinghCommented:
Your program is running perfactly fine. The jar file doesn't contain any
entry called 'images/actors/29.buy_stand.png'
0
 
rainglenAuthor Commented:
Oh, sorry, that was a typo. It's 'images/actors/20/boy_stand.png'

That typo is not in my actual code, just in the initial question I wrote, to make things clearer. Sorry it made things muddier!
0
 
rainglenAuthor Commented:
Ajay, thanks for the vote of confidence but unfortunately my code is not running perfectly fine, as you can see from the error message in my initial question.
0
 
objectsCommented:
> Oh, sorry, that was a typo. It's 'images/actors/20/boy_stand.png'

thats not in there either
0
 
rainglenAuthor Commented:
OK, I just realized you guys are looking at my actual jar on the net. (I'm a little slow here).  Maybe my code is working if I can just get my jar built right. Working on it...
0
 
rainglenAuthor Commented:
OK, my jar is out there, with the correct files in it, I know because my code WORKED (Thanks to all of your helpful suggestion!) from Eclipse IDE. Unfortunately it is not working when I run my applet off the net. So I guess I have a whole new problem. Not sure why the applet running off the 'net can't load from the jar on the net, while from my IDE it does load it from the 'net. Any help with this new related issue would be much appreciated.
0
 
objectsCommented:
it'll need to be loaded from same server applet is loaded from (but if it was it would be far easier to include images in applet jars)
o/wise you'll need to sign your aaplet
0
 
rainglenAuthor Commented:
By the way, to hopefully make the exact line of code relating to the problem clearer:
     URL url = new URL("jar:http://www.rainglen.com/testlaunch/readingpng_actors29.jar!" + ref);
is coming back null when I run my applet from the net or my application via JWS from the net. When I run from my IDE, it comes back with the correct file loaded and works as desired.
0
 
rainglenAuthor Commented:
objects, It is on the same server. It has been my understanding that applets without permissions can only access the server they were loaded from. So I'm hoping there's a way to solve this without signing the jars as that opens a whole can of worms that I am hoping to be able to avoid, at least for a good while longer. I guess this probably won't shed light on the problem, but the problem happens running as an application via Java Web Start as well. So, any ideas why it won't load, despite being on the same server, would be much appreciated.
0
 
rainglenAuthor Commented:
By the way, my applet is accessing the server with no problems via other code I have that reads and writes data to the server.
0
 
rainglenAuthor Commented:
OK, I'm accepting solutions and posting my new problem as a new question. Thanks for the help guys.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now