Getting File from .jar using .getResource()

前端 未结 2 1155
礼貌的吻别
礼貌的吻别 2021-01-27 19:21

I\'m trying to acces a File inside the .jar. In Netbeans, this:

System.out.println(new File(this.getClass().getResource(\"Image.jpg\").getFile()).exists());


        
相关标签:
2条回答
  • 2021-01-27 20:05

    You don't get a File object to it. Its an entry in a JAR not a File on the Filesystem. You have a few options. Create a File by copying the InputStream to the Filesystem, FInd a way to use the InputStream instead, or always have your JAR expanded.

    0 讨论(0)
  • 2021-01-27 20:24

    The file is packed, try

    resource.getInputStream() 
    

    or

    InputStream streamOfYourImg = this.getClass().getRessourceAsStream("com/package/resources/image.jpeg");
    

    Hope it helps

    0 讨论(0)
提交回复
热议问题