views:

41

answers:

1

hey all , i have an application depends on plugins i have a case when i want to load an image from one of my plugins jar

the only way to load the image is

ClassFromPLugin.class.getClassLoader().getResource("image.png");

how can i load the image without using the ClassFromPlugin but use current Component class who need that image

i am loading my jars at runtime , and this specific component who want to load the image don't know where it came from , he just get the image location

thank you

A: 

You must use the ClassLoader with which you loaded the jar containing the image.

Maurice Perry
how can i do so i am using a new class loader each time i am detecting an plugin URL jarfile = new URL("jar", "", "file:" + jarFile.getAbsolutePath() + "!/"); URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { jarfile });i dont know which one is the correct one
shay
i extends the URLClassLoader ,and make a public method for public void addURL(URL url) so each time instead of creating a new ClassLoader for jar i am using the same classLoader public class TestClassLoader extends URLClassLoader{ public TestClassLoader() { super(new URL[0]); } public void addURL(URL url){ super.addURL(url); }}is this a correct way to do it ?is there disadvantage for this way i can guess there must be a reason why sun always creating a new class loader
shay
Sounds like a good idea.
Maurice Perry
Note that you can also chain the ClassLoaders
Maurice Perry
why sun didnt make it that way there is a reason for that ,is that a bad implementation ?
shay