Search code examples
javapathpng

Java how to get path of picture on .jar


I want to get the picture path in .jar. I have opened my app (.jar) as a winrar, and I found my picture, but path of this file not works:

BufferedImage myPicture = ImageIO.read(new File("/windows.png"));
    JLabel picLabel = new JLabel(new ImageIcon(myPicture));

Image file

Image winrar

It means it works in IntelliJ IDEA but not as app. So what is this file path(as app, not IDE)? Thanks for help and sorry for my english!


Solution

  • Try this

    InputStream in = this.getClass().getResourceAsStream("/SomeTextFile.txt");
    

    You can find more detailed answer eg. here How to really read text file from classpath in Java