rainglen
asked on
How do I read .png in jar file from url on same server as the applet I am trying to read the .png from?
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_sta nd.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.
URL url = null;
String ref = "/images/actors/29/boy_sta
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.
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 );
you can then access it the same way you access other images, eg
url = getClass().getResource(ref
> 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
would not seem possible that it is null actually :)
check your java console for why its failing
Your problem is that the URL is wrong. You can't 'read' the whole jar - you need to read an entry from it
Ignore that last - missed the append
ASKER
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.
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.
Check the ref is correct
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I can check the applet out too if you post the link
ASKER
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
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
ASKER
'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.getStoryLev el();
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!
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.getStoryLev
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!
>>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
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
The Swing way is to call
frame.setDefaultCloseOpera tion(JFram e.DISPOSE_ ON_CLOSE);
which is what you should probably do unless you've got special closing requirements when you need to handle it yourself
frame.setDefaultCloseOpera
which is what you should probably do unless you've got special closing requirements when you need to handle it yourself
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> 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.
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.
sounds like what you should be doing is not jarring up the images at all, and just putting them in your apps classpath
ASKER
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.
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.
> 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
yes
ASKER
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.
PS. I will probably post my applet exiting issue as a new questions, thanks for bringing it up.
I told you earlier it would work fine :) and that the problem was not with that line.
>>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?
;-)
That's why i said
>>Don't remember seeing that. Sure you've updated your applet?
;-)
String ref = "/images/actors/29/boy_sta
BufferedImage sourceImage = ImageIO.read(getClass().ge