How to get a path to a resource in a Java JAR file

前端 未结 16 2327
时光说笑
时光说笑 2020-11-22 09:23

I am trying to get a path to a Resource but I have had no luck.

This works (both in IDE and with the JAR) but this way I can\'t get a path to a file, only the file

16条回答
  •  日久生厌
    2020-11-22 09:41

    You need to understand the path within the jar file.
    Simply refer to it relative. So if you have a file (myfile.txt), located in foo.jar under the \src\main\resources directory (maven style). You would refer to it like:

    src/main/resources/myfile.txt
    

    If you dump your jar using jar -tvf myjar.jar you will see the output and the relative path within the jar file, and use the FORWARD SLASHES.

提交回复
热议问题