且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

加载jar中包含的资源

更新时间:2022-01-02 01:54:18

听起来你正在尝试使用 FileInputStream 或类似的东西。不要这样做:而不是调用 getResource ,调用 getResourceAsStream 并从中读取数据。

It sounds like you're then trying to load the resource using a FileInputStream or something like that. Don't do that: instead of calling getResource, call getResourceAsStream and read the data from that.

(您可以从URL加载资源,但调用 getResourceAsStream 会更方便。)

(You could load the resources from the URL instead, but calling getResourceAsStream is a bit more convenient.)

编辑:看过你的更新答案后,似乎其他代码依赖于文件系统中物理单个文件中的数据。因此,答案不是首先将其捆绑在jar文件中。你可以检查它是否在一个单独的文件中,如果没有将它提取到一个临时文件中,但这是非常hacky的IMO。

Having seen your updated answer, it seems other bits of code rely on the data being in a physical single file in the file system. The answer is therefore not to bundle it in a jar file in the first place. You could check whether it's in a separate file, and if not extract it to a temporary file, but that's pretty hacky IMO.