3

I would like to create a separate folder for my resources in a Java project. How, in the name of God do I achieve such a simple thing ? I have been trying to get it to work for quite some time now and it starts to drive me nuts. Here is what I have tried so far:

1) I opened my Build Path Settings and added a folder to my source folders called "resources".

2) I added two folders to that folder called "font" and "img" containing images and font files.

3) I try to access those files in code like this:

getClassLoader() is said to give me an absolute path, so I take a path without the leading /:

getClass().getClassLoader().getResourceAsStream("resources/font/MyFont.otf") 

which returns null. Why is file management in eclipse so infuriating ?

9
  • 1
    Is "resources" outside the "src" folder? Commented Jan 2, 2014 at 14:12
  • @gtgaxiola Yes and I would like to keep it like that. Commented Jan 2, 2014 at 14:14
  • ../resources/font/MyFont.otf Commented Jan 2, 2014 at 14:15
  • 2
    Did you change your build path to src/resources..? In that case you need to give path as font/MyFont.otf Commented Jan 2, 2014 at 14:17
  • as mentioned above, since you are not in the "src" folder, you need to go a level up or some levels up depending on the location of the resource you want to load: getResourceAsStream("../../resources/font...); Commented Jan 2, 2014 at 14:17

3 Answers 3

1

If you have added 'resources' folder as a source folder in the build path settings you should be able to load the fonts like below.

getClass().getClassLoader().getResourceAsStream("font/MyFont.otf") 

Once you specify the 'resources' folder as a source folder eclipse included it in the root of the class path for java. So you only need the path relative to the resources folder.

Sign up to request clarification or add additional context in comments.

Comments

1

I have a solution for you. Should work

Step 1:

InputStream is = this.getClass().getClassLoader().getResourceAsStream("font/MyFont.otf"); 

Step 2:

In Eclipse

run > run configurations > classpath > select user entries > advanced > add folders > select resource folder 

After this Run the project. The reason is the resource folder needs to be in classpath.

Comments

0

try this

getClass().getClassLoader().getResourceAsStream("/font/MyFont.otf") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.