Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Another Jar Question:  (Read 2428 times)
0 Members and 1 Guest are viewing this topic.
Offline nonnus29

Senior Member




Giving Java a second chance after ludumdare fiasco


« Posted 2004-02-16 00:52:42 »

I've been using the loadImage (uses Imageio.read(getClass().getResource()) class from the wiki in my java projects lately.  I'd been loading images in a jar from an app inside a jar with no problems.

Now I'm working with an applet inside a jar.  The jar contains the applet, some misc class files, an image file (.gif), and another jar.  The manifest only has ClassPath defined for the jar in the jar.  The applet loads and runs fine except the images are smeared.  

It makes me think that the getClass.getResource isn't recognizing the image is in a jar when in fact it is.

The file structure in the jar looks like this:

Manifest.mf
js.jar
applet.class
misc.class
misc.class
image.gif

I hate to go thru the trouble of opening a url connection to a jar file if I don't have too?  I shouldn't have to, should I?
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #1 - Posted 2004-02-16 02:10:52 »

It should work the same for Applets.  Describe "smeared".  If it wasn't working you should get something like ResourceNotFound or another error loading the image.   It sounds like the problem is something else.

Offline nonnus29

Senior Member




Giving Java a second chance after ludumdare fiasco


« Reply #2 - Posted 2004-02-16 03:41:30 »

This is really bizarre, every time I compile I repack the jar; I run the applet from the source directory and then from the jar using different html.  So its the same code giving different results:

http://www.geocities.com/nonnus29/javademo2/smear.gif

I tried putting the image in a subdirectory in the jar and in the sub jar (which the manifest classpath points to) of the jar but all of those give the same result as above.  Hmmm....

Edit: You'll probably have to copy and paste the link.  No exceptions and no output to the console.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #3 - Posted 2004-02-16 03:43:47 »

Quote
I tried putting the image in a subdirectory in the jar and in the sub jar (which the manifest classpath points to)


Sub-jar?  You can't have a jar inside a jar be part of the classpath.. the manifest is meant to point to other jars in the same place as the first.  And for applet I think you still need an attribute in your Applet tag to download the additional jars.

Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #4 - Posted 2004-02-16 03:48:24 »

The example image is interesting..  Are you not seeing any exceptions?  You aren't accidently covering one up with an empty catch block or something are you?
The fact that you can see generally correct colours in the bad image tells be that it must be finding the image data some how... but maybe the images are being used before they are loaded, or your images are being corrupted in the jar... maybe even you have classpath issues and the images that work are coming from the current directory instead of the jar somehow?  Maybe you are running in different JVMs - like the MS JVM for applets, but the Sun VM for Applications?

Offline nonnus29

Senior Member




Giving Java a second chance after ludumdare fiasco


« Reply #5 - Posted 2004-02-16 03:59:12 »

Ooops, your right;

Quote

The URLs in the Class-Path header are given relative to the URL of the JAR file of the applet or application.


So I removed the sub jar and the gif.  The applet should have been referencing the gif in the applets home directory and the jar.  Everything was fine, the output  of the applet was the same for both the jar version and the non-jar version.

Then I placed the gif in a sub directory in the jar (and recompiled) and the smear returns.  Weird.

No exceptions are blocked out; my code is the ImageLoader straight from the wiki.  Hmm...

The applet is running jre 1.4.2...   jdk is 1.4.0 (!)

Damn, I thought I was compiling with jdk 1.4.2 these past months;  could that cause a problem?
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #6 - Posted 2004-02-16 13:40:56 »

Starting to sound like a JRE bug to me.. maybe you can post a small test case.

Offline nonnus29

Senior Member




Giving Java a second chance after ludumdare fiasco


« Reply #7 - Posted 2004-02-16 14:04:02 »

I downloaded 1.4.2 and recompiled, no difference.   The only thing I've been able to find in the docs is this bit about urlclassloader:

Quote
This class loader is used to load classes and resources from a search path of URLs referring to both JAR files and directories. Any URL that ends with a '/' is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be opened as needed.


So since I'm using JApplet, maybe its giving me a urlclassloader instead of a regular classloader (from getClass.getResource)?  If its assuming the data for the gif is in a jar could that explain the misplaced colors?

I'll try to put something together this evening and post it. Its pretty simple; a JApplet using getClass().getResource() to load an image 1. from the home directory or 2. from a jar.
Offline nonnus29

Senior Member




Giving Java a second chance after ludumdare fiasco


« Reply #8 - Posted 2004-02-16 18:16:54 »

Here's a zip with the source:

http://www.geocities.com/nonnus29/javademo2/bug.zip

The demojar.htm runs it from the jar.
Offline tom
« Reply #9 - Posted 2004-02-16 20:01:26 »

Is there any reason why you use getResource() instead of getResourceAsStream()?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tom
« Reply #10 - Posted 2004-02-16 21:14:13 »

Quote
Is there any reason why you use getResource() instead of getResourceAsStream()?


Doh. Not as it is any better. sorry.

This will work though:
1  
2  
3  
4  
5  
6  
7  
8  
9  
InputStream in = getClass().getResourceAsStream(resource);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
byte buffer[] = new byte[1024];
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) != -1) {
      byteOut.write(buffer, 0, bytesRead);
}
byte imageData[] = byteOut.toByteArray();
BufferedImage src = ImageIO.read(new ByteArrayInputStream(imageData));


So it looks like a bug in imageio. Maybe in MemoryCacheImageInputStream.

Offline princec

JGO Kernel


Medals: 284
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #11 - Posted 2004-02-17 09:18:08 »

The URL returned by getResource() probably does not have an associated codebase with it for the applet, so it won't work with applets. Use getResourceAsStream() and you should be perfectly fine.

Cas Smiley

Offline nonnus29

Senior Member




Giving Java a second chance after ludumdare fiasco


« Reply #12 - Posted 2004-02-17 10:59:05 »

Quote
The URL returned by getResource() probably does not have an associated codebase with it for the applet, so it won't work with applets. Use getResourceAsStream() and you should be perfectly fine.

Cas Smiley


Quote
Is there any reason why you use getResource() instead of getResourceAsStream()?


Doh. Not as it is any better. sorry.


Both Tom and I tried that it gives the same result.
Offline princec

JGO Kernel


Medals: 284
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #13 - Posted 2004-02-17 11:36:08 »

Rereading the first post I see you're attempting to do something strange as well; you can't access jars inside jars, if you see what I mean - the only resource you have available is the embedded jar. You'll have to open the jar and read data out of it in a separate step.

Cas Smiley

Offline nonnus29

Senior Member




Giving Java a second chance after ludumdare fiasco


« Reply #14 - Posted 2004-02-17 12:19:12 »

Quote
Ooops, your right;

Quote:
The URLs in the Class-Path header are given relative to the URL of the JAR file of the applet or application.




So I removed the sub jar and the gif.  The applet should have been referencing the gif in the applets home directory and the jar.  Everything was fine, the output  of the applet was the same for both the jar version and the non-jar version.


Yep, fixed that problem too.

This is the problem which the zip illustrates:

Quote
Its pretty simple; a JApplet using getClass().getResource() to load an image 1. from the home directory or 2. from a jar.


I'll try get codebase() and see if that does anything different.
Offline tom
« Reply #15 - Posted 2004-02-17 12:56:41 »

Have tried with other images, and jpgs works correctly. This image: http://home.halden.net/tombr/misc/grass.gif causes a:
Quote

java.lang.ArrayIndexOutOfBoundsException: 4096
at com.sun.imageio.plugins.gif.GIFImageReader.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at ImageLoader.loadImage(ImageLoader.java:51)
at jsgcApplet.loadimages(jsgcApplet.java:136)
at jsgcApplet.init(jsgcApplet.java:51)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


It is also influenced by the browser cache. I've done the following:
1. Compile and jar with the working workaround (loading data before sending it to ImageIO)
2. Open applet in Internet explorer. The image shows correctly.
3. Change source code to use the bugged imageloader. Compile and jar it.
4. Reload internet explorer using ctrl+reload. The image shows correcly.
5. Close all instances of internet exploerer
6. Opening the same applet again. The image is bugged.

Offline tom
« Reply #16 - Posted 2004-02-17 22:14:47 »

Found it in bug parade:
http://developer.java.sun.com/developer/bugParade/bugs/4764639.html

Offline nonnus29

Senior Member




Giving Java a second chance after ludumdare fiasco


« Reply #17 - Posted 2004-02-17 22:22:14 »

Apparently its well known, a variation of your workaround is all over the swing tutorial (JApplet + icons).   :-/
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (54 views)
2014-04-15 18:08:23

BurntPizza (52 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (150 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!