Resource Loading.

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:

private String[] getResourceListing(Class classs, String path) throws URISyntaxException, IOException {
        URL dirURL = classs.getResource(path);
        if (dirURL != null && dirURL.getProtocol().equals("file")) {
            /*Plain file, simple to list contents*/
            return new File(dirURL.toURI()).list();
        }
        String me = null;
        if (dirURL == null) {
            /*JAR file.*/
            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:

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.

This might be helpful:
http://www.java-gaming.org/index.php/topic,21435.0.html

OK I found the source of the problem, not sure how to resolve it though. jarPath is a URL to the jar file on the server, not the local cached jar.

User.dir also just returns User.home so that is of no use.

Does anyone know how I might go about locating that? Or perhaps a better way to dynamically load resources from a JWS downloaded jar file.

You can put your resources in a JAR with a class, then do… TheClass.class.getProtectionDomain().getCodeSource().getLocation();

I gave the above code a whirl and in this specific case getCodeSource() is returning null. I read the documentation and various questions on google and that appears to just happen sometimes “Depending on classloader configurations”.

I have a few other things to try before I give up and make a textfile that contains a list of all the files to load. Although that seems a little awkward and un-necessary.

Thanks for the code above though. I would be curious to see how frequently it works on other systems, and will investigate that too.