Problem with directories in jar files

0

I have the following problem, I am trying to access the images of my application from a jar file, the problem is that when I run the program (NOT the JAR file) only the .java file, access the images, but when I package the program in the .JAR and execute it, the images are not shown, we know that this is due, the images are in a folder that is inside the folder src , the folder the name "imagenes" and the classes are inside the main package within the same src . I am using intellij IDEA.

Here is the code of how I have the routes:

guardar = new JMenuItem("Guardar", new ImageIcon("src/imagenes/save.png"));
    
asked by Brian Hernandez 11.04.2018 в 01:39
source

1 answer

0

Since src is not a normal folder, but one within the project, you can do it that way, this is an example to do a ImputStream and to read the image you can modify it to adapt it to the project

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("/imagenes/save.png");
Image logo = ImageIO.read(input);
    
answered by 11.04.2018 в 21:55