0

I want to create a Java class that I use to export images. This class will be exported in .JAR file which will allow me to integrate it directly into another project by passing as input parameter the name of the file and in return I would have this image in PNG format.

That's what I tried:

package militarySymbolsLibrary;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class MainEntry {

    public static void main(String agrs[]) {


    }



    public static BufferedImage generateImage(String symbolCode) {

        File imageFile = new File("/pictures/SSGPU-------.png");

        BufferedImage image;

        try {
            image = ImageIO.read(imageFile);
        } catch (IOException e) {
            image = null;
            e.printStackTrace();
        }

        return image;
    }

}

Pictures are stocked in folder pictures like this treeview below:

src MainEntry.java pictures SSGPU-------.png

Then I export this class in .JAR file, I add it to my other program, I pass a parameter (picture's name except for this test) and normally my class return PNG file. But I have an error telling me that the file path is not the rigth one.

How can I solve this problem?

Thank you

ValentinDP
  • 323
  • 5
  • 14
  • By putting a slash in front of the first directory/folder, you are specifying that the folder is at the root of the filesystem -- is that what you meant? – arcy Nov 25 '19 at 15:47
  • 2
    Images will be converted to [`embedded-resources`](https://stackoverflow.com/tags/embedded-resource/info) when you put them into a JAR file, so it's wise to start threating them as such. See these [example](https://stackoverflow.com/a/42449682/2180785), [example](https://stackoverflow.com/a/34232097/2180785), [example](https://stackoverflow.com/a/44637076/2180785) and some [more examples](https://stackoverflow.com/search?q=user%3A2180785+getResource) to guide you. – Frakcool Nov 25 '19 at 15:51
  • @Frakcool Ok thanks i will check that – ValentinDP Nov 25 '19 at 15:55

0 Answers0