File paths & ResourceLocatorTool in Java

Hi all



This is partly a jME question, and partly a general Java question as I'm still fairly new to this language.



I've noticed the tendency to use jME's ResourceLocatorTool to get access to files on certain paths. I notice that addResourceLocator() takes a String indicating what kind of resource it is, and then apparently a path, which can be a Classpath or a normal path (if we use SimpleResourceLocator).



I'm still kind of trying to wrap my head around this method of doing things, whether I'm doing things the right way or not, and what other sort of options/avenues exist within the scope of ResourceLocatorTool.



Firstly, what purpose does it serve to have the tool take a String type indicating what type of file this is? I presume this is for jME's internal use in other places?



Next, what gap in the Java core does ResourceLocatorTool fill? Why is it necessary (or at least frequently used) within jME but not outside of it? Or is there a similar solution in plain vanilla Java?



Another question I have is re the use of ClasspathResourceLocator. I've just noticed that niftyGUI does this, and this has kind of brought me to a slightly better comprehension of the ResourceLocatorTool which is why I'm now posting. My question is, considering that a game ultimately must be packaged and then deployed, is it smart to use ClasspathResourceLocator during development? Or is it better to just use a SimpleResourceLocator to point to a location relative to the root of your project's /bin folder?



Lastly the overarching question here is, "Although I've already kinda bought into the concept, can you please resell it to me so I can see the true reasoning behind the ResourceLocatorTool?"

Vanilla Solution

It's an upgrade :smiley:


package com.zero.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PathUtil {
   
   private static final Logger logger = Logger.getLogger(PathUtil.class.getName());
   
    public static URL locateResource(String path){
       
       
       String resource_path = PathUtil.class.getClassLoader().getResource(path).getPath().toString();
       
       URL url = null;
      try {
         url = new URL("file://" + resource_path);
         
         if(resource_path.indexOf("!/")> -1 ){
            url = new URL("jar:" + resource_path);
         }
      } catch (MalformedURLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      
      if(!isValid(url)){
         logger.log(Level.SEVERE, "Failed to load URL: " + url);
         url = null;
      }
      
       return url;
    }
   
    private static boolean isValid(URL url){
      try {
         url.openStream();
      } catch (FileNotFoundException e) {
         //e.printStackTrace();
         return false;
      } catch (IOException e) {
         //e.printStackTrace();
         return false;
      }
      return true;
   }
}

DarkPhoenixX said:

Vanilla Solution
It's an upgrade :D


Not meant to be a trick question or anything because I don't have time to do a JavaWS use case right now, but is that Web Start safe?

Don't know :slight_smile:



*.jar works, but I have never tried to us WebStart

Its an issue to think about as it gets quite tricky :frowning:



http://lopica.sourceforge.net/faq.html#customcl