| Author |
Loading all icons from jar file
|
michael modenese
Greenhorn
Joined: Mar 22, 2006
Posts: 12
|
|
Hello,
i am trying to load ALL icons i have in my jar file.
i start with:
.....
Class clazz = Class.forName("com.xxx.IconSelectionDialog");
String me = clazz.getName().replace(".", "/") + ".class";
dirURL = clazz.getClassLoader().getResource(me);
if (dirURL.getProtocol().equals("jar")) {
String jarPath = dirURL.getPath().substring(6, dirURL.getPath().indexOf("!")); //strip out only the JAR file
JarFile jar = new JarFile(jarPath );
Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
while (entries.hasMoreElements()) {
JarEntry nextEntry = entries.nextElement();
String jarEntryName = nextEntry.getName();
.......
.......
}
this works fine while im am debugging from a local jws installation but fails when i start the software with realwebstart from a link.
the error seems to be that the JarFile jar cannot be found even though it has the proper jarPath. can anyone help ?
is there another way to load resources from a jar 8not single named resources, but kind of all *.jpg or similar)
thank you
michael
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8575
|
|
You really dont need to make it this complicated.
Say you got all your image files under images folder.
You can obtain the URL for the folder by getClass().getResource("images");
You can construct a java.io.File object.
On the file object you can call listFiles() which will give you an array of all your image files.
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
michael modenese
Greenhorn
Joined: Mar 22, 2006
Posts: 12
|
|
hello,
i thought it is a bit complicated. but:
i got the uri/url of my images directory:
jar:file:/d:/jars_unsigned/software.jar!/images
how exactly do i make a file object to be used via webstart ?
with new file(uri) not possible.
thank you for your reply
michael
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8575
|
|
michael modenese wrote:hello,
with new file(uri) not possible.
Check out URL#toString() and URI#toASCIIString()
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
Actually you can't make a File object out of a URL unless it's a URL which referred to a file in the first place. And yours isn't.
The standard way to load a set of resources from a jar (or wherever they may be stored) is to provide a list of those resources in the same jar. Then: (1) read the list as a resource (2) for each entry in the list, read that entry as a resource.
Note that the getResourceAsStream() method returns an InputStream which allows you to read the resource.
|
 |
 |
|
|
subject: Loading all icons from jar file
|
|
|