引用jars中的数据文件 [英] referencing data files in jars
问题描述
我的Java程序引用了很多数据文件。我将它们与src /和bin /一起放在名为data /的顶级目录中。在Eclipse中,对data /和../data/的引用似乎都起作用。当我从命令行运行它时,只有../data/有效。
My Java program references a lot of data files. I put them in a top level directory called data/, along with src/ and bin/. In Eclipse, references to data/ and ../data/ both seem to work. When I run it from the command line, only ../data/ works.
当我将bin /和data /目录放在一个jar中并正确设置入口点时,似乎并不知道我要它访问数据/目录里面 jar。我要使其正常工作的唯一方法是将引用设置为../data/,然后将jar放在bin目录中。这显然对我没有任何好处,因为它不是独立的。
When I put the bin/ and data/ directories in a jar and correctly set the entry point, it doesn't seem to know that I want it to access the data/ directory inside the jar. The only way I've been able to get it to work is by setting the references to ../data/ and placing the jar in the bin directory. This obviously doesn't do me any good, because it's not self-contained.
我需要对引用进行哪些操作才能使其正常工作?
What do I need to do to the references to make them work?
谢谢
推荐答案
我建议您以相对于类路径的方式访问文件,无论是在Eclipse中还是在已编译的JAR中。
I'd recommend you access the files in a classpath-relative way, whether in Eclipse or in a compiled JAR.
您可以通过几种方法来解决,但仅JDK的基本方法是使用Class的 getResourceAsStream()
方法,使您可以访问可以读取的InputStream对象。
There are a few ways you might go about it, but a basic JDK-only approach would be to use Class's getResourceAsStream()
method, giving you access to an InputStream object that you can read in.
这篇关于引用jars中的数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!