Reema
Reema

Reputation: 647

Loading image from a Java file

I have this project structure:

I want to load the pic.jpg in the java file which will then be used in the pdf file generated. I have gone through the answers here but, nothing helped me yet. Possibly I am missing a small thing.

If the pic.jpg was under package, then getClass().getResource("pic.jpg") works absolutely fine. getClass().getResource("/web/image/print/pic.jpg") doesnt work as well. But I want to place all my images under image folder and refer it in the java file.

Upvotes: 0

Views: 126

Answers (2)

user3769706
user3769706

Reputation:

Seems your files are not copied to bin/ directory. Change your build script to copy them to output directory

getClass().getResource() will use class loader to load class, since those files need to be in classpath

Upvotes: 0

Feng Lin
Feng Lin

Reputation: 593

You should get the application runtime path and rebase the path to the folder which images locate.
You can use the code:

String path = new File(".").getCanonicalPath();// or System.getProperty("user.dir")

Upvotes: 1

Related Questions