This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Loading all icons from jar file  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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








 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
michael modenese
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

michael modenese wrote:hello,
with new file(uri) not possible.



Check out URL#toString() and URI#toASCIIString()
 
Marshal
Posts: 24495
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Danger, 10,000 volts, very electic .... tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!