gpt4 book ai didi

java - 从jar文件中获取资源

转载 作者:行者123 更新时间:2023-12-01 16:55:37 25 4
gpt4 key购买 nike

我希望我的 jar 文件能够从自身访问一些文件。我知道如何对 BufferedImage 执行此操作,但这不适用于其他文件。我想要的只是从我的 jar 中提取一些 zipper 。我在eclipse中创建了一个类文件夹,将zip放入其中并使用

    public File getResFile(String name){

return new File(getClass().getResource(name).getFile());


}

获取File实例并提取它。它在 Eclipse 中工作正常,但是一旦我将其导出到 jar 中,它就会显示

Exception in thread "main" java.io.FileNotFoundException: file:\C:\Users\DeLL\Desktop\BoxcraftClient\ClientInstaller.jar!\client.bxc (The filename, directory name, or volume label syntax is incorrect)
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:220)
at java.util.zip.ZipFile.<init>(ZipFile.java:150)
at java.util.zip.ZipFile.<init>(ZipFile.java:164)
at Launcher.install(Launcher.java:43)
at Launcher.main(Launcher.java:33)

我已经花了大约 6 个小时来解决这个问题,但找不到解决方案。请帮忙!

最佳答案

getResource()是有原因的返回 URL ,而不是 File ,因为资源可能不是文件,并且由于您的代码打包在 Jar 文件中,所以它不是文件而是 zip 条目。

读取资源内容的唯一安全方法是 InputStream ,或者调用 getResourceAsStream()或调用openStream()返回URL .

关于java - 从jar文件中获取资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33264866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com