Possible Duplicate:
Loading resource bundles using a custom class loader
I’ve made a custom class loader which downloads a jar from my website, and then runs it.
The problem is: my (downloaded) jar needs some resources such as png images, etc…
If I run my (downloaded) jar directly at a command prompt, it works. The resources are loaded like that:
getClass().getResource("/images/logo.png")
Now, if I run it with my custom class loader, using:
Class<?> c = classLoader.loadClass("MainClass");
c.getMethod("main", new Class[] { java.lang.String[].class}).invoke(null, new Object[]{new String[] {"arg1_goes_here"}});
I get a null exception because it can’t load the resource.
When I load my jar file into memory, I do something like that:
String name = entry.getName();
if(name.endsWith(".class")) {
name = name.substring(0, name.length() - 6).replace('/', '.');
classbytes = getResourceData(input);
classes.put(name, classbytes);
} else {
classbytes = getResourceData(input);
if (name.charAt(0) != '/') {
name = "/" + name;
}
resources.put(name, classbytes);
}
I’m loading my class using:
defineClass(classToLoad, buffer, 0, buffer.length, null);
where buffer is the byte array containing the code.
So my question is the following: Is there a kind of defineResource() in Java?
Thanks.
Problem solved,
I’ve written my own method
public InputStream getResourceAsStream(String name).Here is the code:
resourcesis a Map that contains my resource data.Then, in my code, I can use the inputstream to get back my resource. Easy and safe.