IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 1 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Java question...
OK, so I'm going through some code here at work, when I run across the following class:
public class BlankIcon { public BlankIcon() {} }

When I track down how this class is used, I find it's used like this:
BlankIcon.class.getResource("name_of_file_to_load");

After talking to Scott and a few other folks online, they pointed me to the java.lang.ClassLoader class. Specifically, the ClassLoader.getSystemClassLoader().getResource() method. I replaced all instances of the BlankIcon code above with calls to:
ClassLoader.getSystemClassLoader().getResource("path/to/name_of_file_to_load");

So, I made 2 changes. First, changing to the ClassLoader's getResource() method, and changing from just providing the name of the file to providing the full path to the file. The second change is necessary because the old BlankIcon class used to reside in the same package as the files to be loaded.

So I go and test my wonderful new code from within my IDE (JBuilder4 on Linux). Works like a charm. I check in my code to CVS. Other developers grab the code, test it, use it, it works great from within their IDEs (all the same environment).

Release testing day comes. We package everything up into a JAR file. We deploy the JAR to our testing site. We test the JAR applet.

Shit breaks left and right. And where does it break? At the calls to ClassLoader.getSystemClassLoader().getResource("path/to/name_of_file_to_load"); of course.

This was last Thursday, and I'll be damned if I can remember the exact error that was getting thrown, or else I'd post it here. However, I'm wondering if anyone else has run across similar problems, and if so, what's the best route to fix them?

For now, we've gone back to the old BlankIcon.class.getResource() style of doing things. As one of the other programmers here says, this is a "squinky hack." Why doesn't the ClassLoader way of doing things work inside a JAR?
-YendorMike

"The problems of the world cannot possibly be solved by the skeptics or the cynics whose horizons are limited by the obvious realities. We need people who dream of things that never were." - John F. Kennedy
New is .jar in the classpath?`
in order to find the class within a jar, the jar file itself must be declared within the class path. That is, having the jar simply be in a directory that happens to be within the class path is not sufficient.

Other than that, I stay away from Java GUI tools and just use a text editor and command lines for testing.
New An applet?
Aren't applets restricted from accessing that kind of information?
French Zombies are zapping me with lasers!
New Yes, an applet
And the file that it's trying to load is a .jpg file for a button, and said .jpg file is included in the .jar being distributed in the first place. So it shouldn't have a problem acessing a file inside the same jar.
-YendorMike

"The problems of the world cannot possibly be solved by the skeptics or the cynics whose horizons are limited by the obvious realities. We need people who dream of things that never were." - John F. Kennedy
New re: applet restrictions
>> Aren't applets restricted from accessing that kind of information? <<

With enuf certificates or whatever Sun calls those things, applets can be given access to all kinds of stuff. I don't know what the upper limit is, maybe full Java application-like access. My observation is that certificates are a pain in the butt to setup though.

________________
oop.ismad.com
New Re: Java question...
I suspect that the system class loader is either off-limits for the applet or it does not know about applet's jar file. I am afraid that the old way to do it was more appropriate: the class loader from your own class knows about your .jar. I think you can also use getClass().getClassLoader() in any non-static method.
New The reason it doesn't work anymore
Is that your applet is likely using two different class loaders. The system class loader loads the local java base stuff, but a different class loader loads the applet jar (probably some url class loader thing).

BlankIcon.class.getResource("name_of_file_to_load");

is using whatever class loader loaded BlankIcon - which likely isn't the system class loader.
     Java question... - (Yendor) - (6)
         is .jar in the classpath?` - (ChrisR)
         An applet? - (wharris2) - (2)
             Yes, an applet - (Yendor)
             re: applet restrictions - (tablizer)
         Re: Java question... - (Arkadiy)
         The reason it doesn't work anymore - (tuberculosis)

I invited her up to my place for a little midnight bait. I said, "Come on, baby. It'll only take a few minnows." She threw me that same old line: "Not tonight, I've got a haddock."
52 ms