0

I am trying to export to a runnable JAR of my project that has the following structure:

Project
   |
   |---Source folder
   |         |
   |         |
   |     .java files
   |
   |
   |---Image folder
             |
             |
         .png files

The .png images are read inside the .java files using ImageIO.read(new File("ImageFolder/image.png")); which works fine when I run the program in Eclipse, but if I export to a runnable JAR, I get the following exception in the stacktrace:

javax.imageio.IIOException: Can't read input file!                                                              
        at javax.imageio.ImageIO.read(Unknown Source) 

Here is a screenshot of my project in Eclipse:

enter image description here

How can I include the cardImages folder in my JAR so that new File("cardImages/10C.png") can be read?

KOB
  • 4,084
  • 9
  • 44
  • 88
  • Have you tried making cardImages a source folder? – Ray May 02 '17 at 11:46
  • export with directory, read the image files using stream, **getClass().getResourceAsStream('cardmages/imagename')**. – subash May 02 '17 at 11:49
  • @Ray that worked as a temporary fix anyway, but the answers below are definitely the correct way to do it – KOB May 02 '17 at 12:07
  • take a look at here. http://stackoverflow.com/questions/13937904/java-writing-to-txt-in-a-jar-file. really that is worst practise. – subash May 02 '17 at 13:07

2 Answers2

0
  • Right click on your project
  • Select "Build path" => "Configure Build Path..."
  • Check your "cardImages" folder in the new opened window

It should be extracted inside your jar.

Just a tip, for resources prefer the use of

    InputStream is = ClassLoader.getSystemResourceAsStream("resources/sample.txt");
H. Saoud
  • 63
  • 5
  • And what about then writing an image? My program is loading an image from the`cardImages` folder, creating a new image using the one it loaded, and then writing the new image back into `cardImages` `ClassLoader.getSystemResourceAsStream` can only be used for reading as it returns an InputStream – KOB May 02 '17 at 12:33
  • There must be a hack do write files inside a jarfile, but it is not recommended to modify a jar at Runtime, you could encounter many issues. If you have to write file, the best practice is to create a folder in the same directory as the jar, and manage your file there (like putting configuration file or cardImages) You can do it by : C:User/adom/documents/jarName.jar!/fileName.txt Putting the "!" character to assume it is a jarfile. – H. Saoud May 05 '17 at 11:32
0

export with directory, read the image files using stream, getClass().getResourceAsStream('cardmages/imagename');

normally runnable jars, file operations are realtive to its path, where its getting excecuted. for ex

c:/myFolder>yourjar.jar

then your file trying to read from, c:/myFolder/cardimages/imagename.

File is a class used to access files from file system. not from inside a jar.

subash
  • 3,116
  • 3
  • 18
  • 22
  • So I'd call it as `ImageIO.read(getClass().getResourceAsStream('cardImages/10C.png');)` – KOB May 02 '17 at 12:29
  • My program also needs to write images to the same folder, how can this be done in a similar way? – KOB May 02 '17 at 13:02