Could not find an appropriate subforum to put this in so I decided on here:
I am trying to list all the files in a directory, regardless of the location of that directory (in a jar file, or just a plain system directory). The use of this is dynamically loading resources and not having to change a huge hard-coded list in between versions. So far my code works splendidly when launching from a jar or just launching form the filesystem. All files are found and loaded correctly.
However, when I distribute the jar file via java web start, it has trouble opening the jar file. I think perhaps my understanding of how java web start works is incorrect, and I will post this question in a more appropriate location if I can't figure out out here.
Here is the offending code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| private String[] getResourceListing(Class classs, String path) throws URISyntaxException, IOException { URL dirURL = classs.getResource(path); if (dirURL != null && dirURL.getProtocol().equals("file")) { return new File(dirURL.toURI()).list(); } String me = null; if (dirURL == null) { me = classs.getName().replace(".", "/") + ".class"; dirURL = classs.getClassLoader().getResource(me);
if (dirURL.getProtocol().equals("jar")) { String internalJarPath = me.substring(0, me.lastIndexOf("/")) + "/" + path; String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8")); Enumeration<JarEntry> entries = jar.entries(); Set<String> results = new HashSet<String>(); JarEntry je; while (entries.hasMoreElements()) { je = entries.nextElement(); String name =je.getName(); if (name.startsWith(internalJarPath) && !je.isDirectory()) { results.add(name.substring(name.lastIndexOf("/")+1)); } }
return results.toArray(new String[results.size()]); } } throw new UnsupportedOperationException("Cannot list files for URL "+dirURL); } |
I think the offending line is:
1
| JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8")); |
I am decently sure the jarPath does not accurately point to the jar file launched when launching via a jnlp file. But finding documentation on that kind of thing, has been like pulling teeth.
Any insight into java web start would be greatly appreciated.