Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (687)
Games in Android Showcase (198)
games submitted by our members
Games in WIP (758)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Resource Loading.  (Read 2829 times)
0 Members and 1 Guest are viewing this topic.
Offline Coinerson

Junior Devvie

Introducing the world's cutest zombie, Timmy

« Posted 2010-05-24 05:55:10 »

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()) {

                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.
Offline Nate

« JGO Bitwise Duke »

Medals: 167
Projects: 4
Exp: 14 years

Esoteric Software

« Reply #1 - Posted 2010-05-24 11:32:59 »

This might be helpful:

Offline Coinerson

Junior Devvie

Introducing the world's cutest zombie, Timmy

« Reply #2 - Posted 2010-05-24 18:38:39 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nate

« JGO Bitwise Duke »

Medals: 167
Projects: 4
Exp: 14 years

Esoteric Software

« Reply #3 - Posted 2010-05-24 18:41:45 »

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

Offline Coinerson

Junior Devvie

Introducing the world's cutest zombie, Timmy

« Reply #4 - Posted 2010-05-25 04:32:39 »

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.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

roseslayer (573 views)
2016-08-06 11:43:29

roseslayer (529 views)
2016-08-06 09:43:11

xTheGamerCodes (601 views)
2016-08-04 15:40:59

xTheGamerCodes (596 views)
2016-08-04 15:40:24

orrenravid (942 views)
2016-07-16 03:57:23

theagentd (1020 views)
2016-07-11 14:28:54

Hydroque (1125 views)
2016-07-06 05:56:57

Hydroque (1070 views)
2016-07-03 08:52:54

GrandCastle (888 views)
2016-07-01 09:13:47

GrandCastle (647 views)
2016-07-01 09:09:45
Rendering resources
by Roquen
2016-08-08 05:55:21

Rendering resources
by Roquen
2016-08-08 05:52:42

Rendering resources
by Roquen
2016-08-08 05:50:38

Rendering resources
by Roquen
2016-08-08 05:49:53

Rendering resources
by Roquen
2016-08-08 05:32:39

Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!