?
Solved

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

Posted on 2007-09-30
22
Medium Priority
?
270 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.  
0
Comment
Question by:rainglen
  • 10
  • 7
  • 5
22 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 19989115
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));
0
 
LVL 92

Expert Comment

by:objects
ID: 19989178
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);
0
 
LVL 92

Expert Comment

by:objects
ID: 19989188
> 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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 19989232
Your problem is that the URL is wrong. You can't 'read' the whole jar - you need to read an entry from it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19989268
Ignore that last - missed the append
0
 

Author Comment

by:rainglen
ID: 19990669
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19990702
Check the ref is correct
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1400 total points
ID: 19990756
Your image is readable by me. so not quite sure why not by you
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19990889
I can check the applet out too if you post the link
0
 

Author Comment

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

thanks much!

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19991776
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
0
 

Author Comment

by:rainglen
ID: 19993063
'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!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19993123
>>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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19993147
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
0
 
LVL 92

Assisted Solution

by:objects
objects earned 600 total points
ID: 19994662
> '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:

calling it won't cause u any problems, (itr won't do a thing)

if url is null it can only be calling:

            url = getClass().getResource(ref);
0
 
LVL 92

Expert Comment

by:objects
ID: 19994688
> 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.
0
 
LVL 92

Expert Comment

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

Author Comment

by:rainglen
ID: 19996168
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 19996213
> 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

0
 

Author Comment

by:rainglen
ID: 19996357
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 19996376
I told you earlier it would work fine :) and that the problem was not with that line.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19996982
>>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?

;-)
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question