Accessing a resource within a jar (working in Netbeans but not on command line)

前端 未结 2 563
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-26 21:07

I have a jar file with bundled resources (language model binary files) that need loading at run time. The directory structure within the jar is

tagger/app.class
tag

相关标签:
2条回答
  • 2021-01-26 21:15

    You can't access a resource in a Jar file via java.io.File. You should provide an alternate constructor for SentenceDetector which either accepts an InputStream rather than a String, or which accepts an URL and uses URL.openStream() to get an InputStream.

    0 讨论(0)
  • 2021-01-26 21:18

    It sounds like that should be fine, if the binary is definitely in the right place and the jar file is definitely in the class path. (I assume it can find the class itself? I'd expect so, given that you're using this.getClass()).

    One thing which might be causing issues is filename casing - when it's loading the file from the file system, if you're using Windows that'll be case insensitive; in a jar file it'll be case sensitive. Check the case in code matches the case in the jar file.

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