0

My application uses some image files. I added them manually to the artifact under File -> Project Structure -> Artifacts -> Output Layout. If I build the artifact and open it with an archive manager I can see that the image files are there under the root path.

How can I load them in the Java code? I have tried both the simple path (like "image.png") and getClass().getResource(), as well as moving the files under different locations but nothing seems to work.

Shepard
  • 801
  • 3
  • 9
  • 17

2 Answers2

0

You have two options: 1) Use System.getProperty("user.dir") to get the current user dir, it could be your project root path or some system path. Then you can put files there (see #1)

2) Use Classloader ClassLoader.getResource(String name). Because of parent delegation model, JVM first look the file up in the classpath of the parent classloader. If you use IDEA, the simplest way is to create a new project using maven. A resources folder will be created under main folder. enter image description here

Community
  • 1
  • 1
bresai
  • 369
  • 5
  • 18
  • There is no resources directory, I tried to create and add it under some different paths but it doesn't work. – Shepard Dec 24 '16 at 21:25
0

I had to put the resources (the 'image' folder) in the same package (in my case 'gui') of the classes that use them, then invoke getClass().getResource("images/icon.png").

Shepard
  • 801
  • 3
  • 9
  • 17