Create a Valid Url Object

Hi

I try to create a URL Object to load all my game resources.

The Method for this:


public static URL getFileURL(String aFile) {
  return getURL("file:"+aFile);
}

This Method works just fine. But i thought it would be bette to create the URL Object via class Loader. So i rewrote it:


public static URL getFileURL(String aFile) {
return new Resource().getClass().getClassLoader().getResource(file);
}

This Method doesn’t work… Can someone tell me why?

Thanks & good night

greets from Switzerland

Should that be:

return getClass().getClassLoader().getResource(file); 

The Method is static so you can not access getClass directly.

I think the Directory for the resource must be in the classpath. I tried that but it doesn’t work.

I use the following…

URL url = ThisClassName.class.getResource( “/directory/filename”);

which seems to work, including over webstart.

Hmm, is the directory a sub directory from a Package or is the directory in your classpath? On my machine it doesn’t work and i can’t see why. )-:

The directory is relative to the classpath.

I don’t get it. Ok, let’s make an example

you have a Project with the following structure:
src
images <— resources

In your game you wright “/images/image.png”

Now we assume you have your Project in D:\projects
so the classpath would be D:\projects\project
If the jar is in that directory, with the that classpath, it should work… right?

Btw, sorry for my bad english … and my stupidity (-:

greets