Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (416)
games submitted by our members
Games in WIP (306)
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 1957 times)
0 Members and 1 Guest are viewing this topic.
Offline Coinerson

Junior Member




Introducing the world's cutest zombie, Timmy


« Posted 2010-05-24 07: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:
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")) {
            /*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:
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.
Offline Nate

JGO Wizard


Medals: 88
Projects: 3


Esoteric Software


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

This might be helpful:
http://www.java-gaming.org/topics/how-to-check-if-a-url-is-a-directory/21435/view.html

Offline Coinerson

Junior Member




Introducing the world's cutest zombie, Timmy


« Reply #2 - Posted 2010-05-24 20: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 Wizard


Medals: 88
Projects: 3


Esoteric Software


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

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

Offline Coinerson

Junior Member




Introducing the world's cutest zombie, Timmy


« Reply #4 - Posted 2010-05-25 06: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
mrbenebob (8 views)
2013-06-19 14:55:23

BrassApparatus (15 views)
2013-06-19 08:52:37

NegativeZero (19 views)
2013-06-19 03:31:52

NegativeZero (21 views)
2013-06-19 03:24:09

Jesse_Attard (26 views)
2013-06-18 22:03:02

HeroesGraveDev (64 views)
2013-06-15 23:35:23

Vermeer (63 views)
2013-06-14 20:08:06

davedes (63 views)
2013-06-14 16:03:55

alaslipknot (57 views)
2013-06-13 07:56:31

Roquen (79 views)
2013-06-12 04:12:32
Smoothing Algorithm Question
by UprightPath
2013-05-28 02:58:26

Smoothing Algorithm Question
by UprightPath
2013-05-28 02:57:33

Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38
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!