How to pack resources in a Maven Project?

前端 未结 3 1721
独厮守ぢ
独厮守ぢ 2020-12-15 13:54

I am looking for suggestion in putting image file maven web project. One of classes under src/main/java need to use an image file. My problem is, if i put image file under s

3条回答
  •  长情又很酷
    2020-12-15 14:15

    It seems that your problem is the way you read the image. You can't (or shouldn't) read the image from a Path. The Path might change on every system.

    You need to read the image from the classpath like:

    MyClass.class.getResource("/images/image.jpg")
    

    or

    MyClass.class.getResourceAsStream("/images/image.jpg")
    

    For this to work, the image Folder needs to be on the classpath. Maven will put everything in the classpath, that is under main/resources. So thats where your image folder should be.

提交回复
热议问题