Loading Resources

I've tried a few different things, but none have worked and I'm absolutely clueless.



In all of the JME tutorials, when any resources are used, they're all located within the src.



I've got a second folder named 'Cache' in the project's root folder that I keep my resources in.



Whenever I try to load a resource, however, it's never found unless I move the cache into the src folder as a package.



Does anyone know how to redirect the path to the Cache folder without using an absolute path from "C:/"?



Edit: If it matters, I'm using Eclipse

In most examples you see that an URL is created at some time. This is where the path you see as text in the code is used. What  you should notice is that the path is a classpath, so it only works if the resource is in some jar thats in the classpath.



You can create an URL from a java File object with a file on your system like this:


URL url=new File("c:\my\path\and\file").toURL();



Dont know how it is in Eclipse, but in NetBeans projects always run in the directory of the project, so a relative path from there should work.
normen said:

In most examples you see that an URL is created at some time. This is where the path you see as text in the code is used. What  you should notice is that the path is a classpath, so it only works if the resource is in some jar thats in the classpath.

You can create an URL from a java File object with a file on your system like this:


URL url=new File("c:\my\path\and\file").toURL();





Edit: Alright, I managed to get it with this

         AudioTrack music = AudioSystem.getSystem().createAudioTrack(new File("Cache/Audio/Kizashi.wav").toURI().toURL(), false);



Is this the easiest way to do it? Or would there be a better alternative?

Well then… there you have it :slight_smile: Is File.toURL() really deprecated in Java6? I am using Java5 there its not.

normen said:

Well then.. there you have it :) Is File.toURL() really deprecated in Java6? I am using Java5 there its not.


Thanks for the help mate. I appreciate it. And yes, it is.

Deprecated. This method does not automatically escape characters that are illegal in URLs. It is recommended that new code convert an abstract pathname into a URL by first converting it into a URI, via the toURI method, and then converting the URI into a URL via the URI.toURL method.

Converts this abstract pathname into a file: URL. The exact form of the URL is system-dependent. If it can be determined that the file denoted by this abstract pathname is a directory, then the resulting URL will end with a slash.

Returns:
A URL object representing the equivalent file URL
Throws:
MalformedURLException - If the path cannot be parsed as a URL
Since:
1.2
See Also:
toURI()
java.net.URI
java.net.URI.toURL()
java.net.URL

Does

 new File(".\Cache\Audio\Kizashi.wav").toURL()

not work without the URI workaround? Note the "." before the path.

normen said:

Does

 new File(".\Cache\Audio\Kizashi.wav").toURL()

not work without the URI workaround?


It works, but it's deprecated (And toURI().toURL() was recommended as the alternative via the Documentation.
normen said:

Well then.. there you have it :) Is File.toURL() really deprecated in Java6? I am using Java5 there its not.


There was actually a thread (or maybe it was a mailing list) with people upset/annoyed by this.  If ever there was something trivial to whine about