Within resources in JAR

后端 未结 2 473
星月不相逢
星月不相逢 2020-12-21 02:47

We have a Jar-file. There are 3 folders:

1-st: META-INF

2-nd: resources

3-rd: classes

How

2条回答
  •  一向
    一向 (楼主)
    2020-12-21 03:24

    Here's an example:

    String path = "resources/something.png";
    BufferedImage img = ImageIO.read(getClass().getClassLoader().getResource(path));
    

    To do it in a static context, like in a static initializer block:

    String path = "resources/something.png";
    BufferedImage img = ImageIO.read(className.class.getClassLoader().getResource(path));
    

提交回复
热议问题