java, loading of resources fails: unable to resolve file:/my-jar.jar!/folder/my-file

前端 未结 2 549
小鲜肉
小鲜肉 2020-12-22 02:33

From a java application i made, I build the corresponding jar file.

I copy all the resources in the jar file.

For example, if in src/main/resources

相关标签:
2条回答
  • 2020-12-22 03:10

    In that case, I would try:

    YourClass.class.getClassLoader().getResourceAsStream("folder/my-file");
    

    Which would return an InputStream to your resource independently of your execution environment .

    0 讨论(0)
  • 2020-12-22 03:11

    if you have an structure like this:

    src/main/Begin.java
    src/main/folder/icon.png
    

    Try this in the Begin.java:

      Begin.getClass().getResource("/main/folder/icon.png");
    
    0 讨论(0)
提交回复
热议问题