Help on Relative Paths in Java Mac OSX

Discussion in 'Mac Programming' started by Jigzman, Jul 29, 2008.

  1. Jigzman macrumors newbie

    Jul 29, 2008
    Hello Everyone.

    I am a programmer who just switched from windows to Mac and I am having trouble with relative paths in Java (I'm using Eclipse BTW)...

    In the program I was trying to load images from jpg files. something like:

    bgImage = Resources.getResource("resources/images/bg.jpg");

    I am getting an error, the file cannot be found.

    In windows, this relative path starts from the src folder (.../[Project Folder]/src), so all I had to do was put all my image and other resource files in the src folder.

    I tried placing this image file everywhere (bin folder, project folder, workspacee folder) just to findout where the relative path starts. But the file still can't be found when I run the program.

    My questions are:

    1. Is there anything wrong with how I constructed the relative path? (In windows this works)

    2. In Mac OS, where is the working directory from which the relative path will start?

    Finally, how do I access files and folders without resorting to absolute paths?
  2. MrStevieP macrumors newbie

    Feb 27, 2008
    Path looks ok to me. I believe you can find your working dir from System.getEnv().

    Hope it helps,

  3. wrldwzrd89 macrumors G5


    Jun 6, 2003
    Solon, OH
    Nothing's wrong with the method you're using - you MUST start the path with a / though, even if it's a relative path. I have no idea why Java insists that paths given to it with class.getResource() start with slashes, though.

    Here's an example: If you store your images with the rest of your source code, say in the package, then you would retrieve them like this (assuming the class you're using to manage resources is called Resource):

  4. stadidas macrumors regular

    Feb 27, 2006
    Kent, United Kingdom
    If you want to find out where the path you have specified is actually pointing, you could do this:
    That will probably help you figure out what's going on.
  5. Jigzman thread starter macrumors newbie

    Jul 29, 2008
    I tried reorganizing my src files....

    Thanks for the initial input guys. So now i tried to reorganize my resources and put them in a package in src (as mentioned in one of the posts above)

    I put it in a package named resources.images,

    then I tried this

    bgImage = Resources.getResource("/resources/images/bg.jpg");
    It finally worked. Thanks.
  6. Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
    What JVM are you targeting? I've been working with 1.4.2 and doing a lot of JAR resource loading and that isn't the case; the moment you add that leading '/' it is no longer a relative path but an absolute one.

    As for the actual path, you CAN use something to the order of "SomeClass.class.getResource( "path/to/resource.ext" )" BUT the major thing to note is that it will work from whatever classpath SomeClass exists it. If SomeClass is in the package "some.package" the getResource call will assume you mean "/some/package/path/to/resource.ext"
  7. wrldwzrd89 macrumors G5


    Jun 6, 2003
    Solon, OH
    Strange. I've been using 5, but 1.4.2 should behave in the exact same way.

    Here's an example from Dungeon Diver, a Java 5 application:
    final URL url = GraphicsManager.class.getResource("/net/worldwizard/DungeonDiver/Resources/graphics/logo/logo.png");
    final Image image = Toolkit.getDefaultToolkit().createImage((ImageProducer) url.getContent());
    Once you have an Image object using this method (which DOES work), you can enclose it in an ImageIcon for display, or do just about anything else you would normally do with Image objects.
  8. jeremy.king macrumors 603


    Jul 23, 2002
    Fuquay Varina, NC
    getResource() of the Class class loads from the classpath, not the filesystem. It's useful if you intend to bundle your images with your application jar(s).

    Also, I'm not familiar with that Resources class you are using - or is it just a poorly named variable?

    To use getResource, create a package in your src folder called resources.images and then drop the bg.jpg image into that package. You can now access these from any class instance using:


Share This Page