Jar get image as resource

前端 未结 3 1315
无人共我
无人共我 2020-12-12 01:46

I\'m trying to get load an image from my jar. But no matter what string I supply for getResource() it always returns null.

try {
    System.out.println(Boots         


        
相关标签:
3条回答
  • 2020-12-12 02:28

    From the stack trace I see the Bootstrapper class lies in net.sharpcode.crawler package. It means the following path ./img/logo.png will be resolved to /net/sharpcode/crawler/img/logo.png. I guess /img is a top level directory, so either use absolute path:

    Bootstrapper.class.getResource("/img/logo.png")
    

    or load via ClassLoader:

    Bootstrapper.class.getClassLoader().getResource("img/logo.png")
    
    0 讨论(0)
  • 2020-12-12 02:32
    this.getClass().getResource("/net/sharpcode/crawler/img/logo.png")
    
    0 讨论(0)
  • 2020-12-12 02:36

    Get rid of the "./" part - just use:

    Bootstrapper.class.getResource("img/logo.png");
    

    ... that's if it's relative to Bootstrapper.class. If the img "directory" is in the root of the jar file, use

    Bootstrapper.class.getResource("/img/logo.png");
    
    0 讨论(0)
提交回复
热议问题