Access files packed in jar file

I created a jar file with an executable compiled class and some template files (text). Now I want to use text files packaged in a jar for distribution in the application, as objects File.

I was trying to just use relative paths that didn't work. How to get an object Filepointing to a text file that is in the bank?

Is there a way to do this so that it works both when running unpacked class files and from a jar with the same code, or will I need to use separate functions?

+3
source share
2 answers

from the inner can, you can use it like Class.getResourceAsStream(String)or something like that.

, ,

+5

JAR , Class.getResourceAsStream(String) .

, Servlet, ServletContext.getResourceAsStream(String).

2 InputStream ( ), File .

+1

Source: https://habr.com/ru/post/1789467/