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