class.getClassLoader().getResource();

Good Evening Folks,



I have another quick question… but I'm not sure if this is a general Java problem? I am currently trying to use the following code: http://www.jmonkeyengine.com/wiki/doku.php?id=unified_model_loading. The problem I am having lies with the following line of code:


 URL         modelURL   = ModelLoader.class.getClassLoader().getResource(modelBinary);



This ALWAYS returns a null value to modelURL, regardless of wether the string representing modelBinary is a real file!!! Instead I am using:


// Attempt to link to a binary file
      try {
         File modelBinaryFile = new File(modelBinary);
         if (modelBinaryFile.exists() == true) {
            modelURL = modelBinaryFile.toURL();
         }
      } catch (MalformedURLException e1) {
         e1.printStackTrace();
      }



Which works perfectly! Obviously I would like to use the latter, as it is far less verbose!

I am using Eclipse 3.3.0 (M20070921-1145) on my OS X (Leopard) Java 1.5.0 platform.

Thoughts?

Cheers guys!

You should instead use the ResourceLocatorTool… search the forums for details and usage. It replaces the getResource method, and the best part is that it can locate resources inside JARs.

My files are in the standard filesystem (not JARed up anywhere), so the original JAVA .getResource() should have worked anyway! I really don't understand why it doesnt come up when I specify the relative path of the file!

Because get resource only accesses resources that are in the original classpath of the VM.

if u copy the file into ur project under eclipse it will work fine.



if its in side any package, u can just specify the relative path

The best part about ResourceLocatorTool is that it works for both jars and filesystem. So if you decide to move your files into jars, you will not have to change code, just replace the lookup directories.



The only difference when working with jars is you cannot specify a directory, you actually have to point to an existing file. So when specifying a base directory in SimpleResource locator, point to any file inside that directory.