Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jigzman

macrumors newbie
Original poster
Jul 29, 2008
2
0
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?
 

MrStevieP

macrumors newbie
Feb 27, 2008
22
0
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?

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

Hope it helps,

Steve
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
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?
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 com.company.resources.graphics, then you would retrieve them like this (assuming the class you're using to manage resources is called Resource):

Code:
Resource.class.getResource("/com/company/resources/graphics/image.jpg");
 

stadidas

macrumors regular
Feb 27, 2006
243
0
Kent, United Kingdom
If you want to find out where the path you have specified is actually pointing, you could do this:
Code:
System.out.println(File("mypath/image.jpg").getCanonicalPath());
That will probably help you figure out what's going on.
 

Jigzman

macrumors newbie
Original poster
Jul 29, 2008
2
0
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

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

It finally worked. Thanks.
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
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

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"
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
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"
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:
Code:
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.
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, 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:

Code:
this.getClass().getResource.getResource("/resources/images/bg.jpg");

javadoc
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.