简体   繁体   中英

How do i read an image file from my java application?

i have developed an invoice generate program. each pdf invoice include an image on top left corner, when i run the application on someone else's machine(not the developed machine), it doesn't show the image on top left corner of the pdf.

this is how I read the image:

Image companyLogo = Image.getInstance("images/amadeus14.png");

This is my option 1

within the project i have created a package and inside it i have made a folder called image. inside that folder i have put that image file and tried to access it in my program.

Image companyLogo = Image.getInstance("Resources/Images/HemasLogo.jpg");

this is my option 2 option

but it gives an error saying

java.io.FileNotFoundException: C:\\Users\\businesssupport\\Documents\\NetBeansProjects\\invoiceGenerator\\Resources\\HemasLogo.jpg (The system cannot find the path specified)

Looking at your previous questions this and this ,

  • you must know you're trying to read from current working directory (you can find it using System.getProperty("user.dir")) )
  • It is not a webapp.
  • You cannot use shared (FTP) folders

So, I can see 2 options:

  1. to put the image inside a package before packing the application, then you will be able to read it from any computer without making any changes

    Check Including Images with an executable jar


  1. Use a local folder. You will need to create a common folder for each installed computer, then manually copy the image on each computer, and read it like

     Image companyLogo = Image.getInstance("C:\\\\myApp\\\\images\\\\amadeus14.png"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM