Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
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 2433 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 Kernel


Medals: 145
Projects: 4
Exp: 14 years


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 Kernel


Medals: 145
Projects: 4
Exp: 14 years


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.

 

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

The first screenshot will be displayed as a thumbnail.

CogWheelz (17 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (20 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org 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‑gaming.org
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!