从jar文件获取资源 [英] Get resources from a jar file
问题描述
我想要我的jar文件从本身访问一些文件。我知道如何执行 BufferedImage
,但这不适用于其他文件。我想要的是从我的罐子中提取一些拉链。我在eclipse中创建了一个类文件夹,将拉链放在里面并使用
i want my jar file to access some files from itself. I know how to do this for BufferedImage
but this doesn't work for other files. All i want is to extract some zips from my jar. i made a class folder in eclipse, put the zips inside and used
public File getResFile(String name){
return new File(getClass().getResource(name).getFile());
}
获取文件
实例并解压。它在日食中工作正常,但是一旦我将其导出到一个jar,它说
to get the File
instance and extract it. it works fine in eclipse, but as soon as i export it to a jar it says
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个小时了,找不到解决方案。请帮助!
Im working to fix this already something like 6 hours and can't find a solution. Please help!
推荐答案
有一个原因为什么 getResource()
返回一个 URL
,而不是文件
,因为资源可能不是并且由于您的代码打包在Jar文件中,它不是一个文件,而是一个zip条目。
There is a reason why getResource()
returns a URL
, and not a File
, because the resource may not be a file, and since your code is packaged in the Jar file, it's not a file but a zip entry.
读取资源内容的唯一安全方法是 InputStream
,通过调用 getResourceAsStream()
或调用 openStream()
在返回的 URL
。
The only safe way to read the content of the resource, is as an InputStream
, either by calling getResourceAsStream()
or by calling openStream()
on the returned URL
.
这篇关于从jar文件获取资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!