-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: finding certain files in a jar using ClassLoader.getResource
PostPosted: Fri Nov 26, 2010 4:55 pm 
Beginner
Beginner

Joined: Tue Aug 03, 2010 4:32 pm
Posts: 22
I've built a Java/Hibernate/Swing standalone app that works great when executed from within Eclipse.
I created a runnable jar, in which the GUI part worked but the app stalled when the "DoIt" JButton was clicked.
After a great deal of googling I have a vague understanding that the problem lies with classloaders, and that
one solution is to use ClassLoader.getResource() and/or ClassLoader.getResources()

I know these files are contained in the root directory of the runnable jar:
lamp.cfg.xml
sofa.cfg.xml
desk.cfg.xml

which is to say they are located in the /src directory of my Eclipse project.

a snippet of my HibernateUtil.java:
Code:
URL jvdf  = contextClassLoader.getResource("lamp.cfg.xml");    // 
URL ajbf  = contextClassLoader.getResource("sofa.cfg.xml");     //  these three work great 
URL utqw = contextClassLoader.getResource("desk.cfg.xml");    // 

// but getResources returns null in all these cases -- why? 
Enumeration<URL> bjbj = contextClassLoader.getResources("/"); 
System.out.println("printing the enumeration for slash"); 
while( bjbj.hasMoreElements()) { 
    URL urrl = bjbj.nextElement(); 
    String str = urrl.getFile(); 
    System.out.println("an bjbj:" + str); 

Enumeration<URL> popo = contextClassLoader.getResources("."); 
System.out.println("printing the enumeration for single-dot"); 
while( popo.hasMoreElements()) { 
    URL urrl = popo.nextElement(); 
    String str = urrl.getFile(); 
    System.out.println("an popo:" + str); 

Enumeration<URL> hghg = contextClassLoader.getResources(""); 
System.out.println("printing the enumeration for empty"); 
while( hghg.hasMoreElements()) { 
    URL urrl = hghg.nextElement(); 
    String str = urrl.getFile(); 
    System.out.println("an hghg:" + str); 

Enumeration<URL> yryr = contextClassLoader.getResources("[^.]+\\.cfg\\.xml");    //  <-- this would be awesome if I can get it to work 
System.out.println("printing the enumeration for yryr"); 
while( yryr.hasMoreElements()) { 
    URL urrl = yryr.nextElement(); 
    String str = urrl.getFile(); 
    System.out.println("an yryr:" + str); 



I don't want to have to hardcode filenames - I want the code to find them, either by the regex used in the third Enumeration or by iterating through all the files in the root directory of the jar and picking out the desired files.

So how can I get
a) ClassLoader.getResources() to return a list (well, enum) of just the files in the root directory of the jar that I want, using the regex
b) ClassLoader.getResources() to return a list (well, enum) of all the files in the root directory of the jar


TIA,

Still-learning Stuart


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.