Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java class getResource() with eclipse

i've been trying to get getResource to work correctly this whole morning but so far the only way i've had to get it working is by moving the res folder to the bin folder and then using getResource like so

URL url = MyClass.class.getResource("/res/gfx/entity/entity1.png");

But is there not any way i can make it so res is outside the bin folder?

like image 589
Nicolas Martel Avatar asked Sep 09 '12 17:09

Nicolas Martel


People also ask

How do I find class references in eclipse?

To get there, we go to the menu under Navigate > Open Type in Hierarchy or we use the shortcut: Ctrl + Shift + H on a PC or Cmd + Shift + H on a Mac. This dialog is similar to the Open Type dialog. Except for this time when we click on a class, then we get the Type Hierarchy view.

How do I run a different class in Eclipse?

Step 1: Open Eclipse and click File > New > Java Project. Step 2: Provide the Project Name and click on the Finish button. Step 3: In the Package Explorer (left-hand side of the window) select the project which you have created. Step 4: Right-click on the src folder, select New > Class from the submenu.

How to read all files from resources folder In java?

In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream . // the stream holding the file content InputStream is = getClass(). getClassLoader().


1 Answers

Nevermind, i can't believe how stupid of a fix that was.

I added the res folder to the main project folder rather than bin, then went in eclipse

run > run configurations > classpath > select user entries > advanced > add folders > select res

then when calling a folder from res, rather than doing "/res/gfx/entity/entity1.png", i did "/gfx/entity/entity1.png"

Problem solved!

like image 105
Nicolas Martel Avatar answered Sep 17 '22 19:09

Nicolas Martel