Java 如何访问 jar 中的配置文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/944610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I access a config file inside the jar?
提问by Owen
I'm using FlatPackto parse and load data from flat files. This requires loading a config file that stores mappings of the columns of the flat file.
我正在使用FlatPack从平面文件解析和加载数据。这需要加载一个配置文件,该文件存储平面文件的列的映射。
I have a constant to define the location of the mapping file:
我有一个常量来定义映射文件的位置:
private static final String MAPPING_FILE = "src/com/company/config/Maping.pzmap.xml";
I have a parse(File dataFile) method that actually does the parsing:
我有一个 parse(File dataFile) 方法,它实际上进行解析:
private void parse(File dataFile) throws FileNotFoundException, SQLException {
Parser parser;
log.info("Parsing " + dataFile.getName());
FileReader mappingFileReader = new FileReader(MAPPING_FILE);
FileReader dataFileReader = new FileReader(dataFile);
parser = DefaultParserFactory.getInstance().newFixedLengthParser(mappingFileReader, dataFileReader);
parser.setHandlingShortLines(true);
DataSet dataSet = parser.parse();
//process the data
}
When I jar up everything and run it as a jar - it bombs out on FileReader mappingFileReader = new FileReader(MAPPING_FILE);
with a FileNotFoundException
. That file is inside the jar though.
当我把所有东西都装起来并作为 jar 运行时 - 它会FileReader mappingFileReader = new FileReader(MAPPING_FILE);
用FileNotFoundException
. 不过那个文件在 jar 里面。
How do I get to it?
我该怎么做?
I've looked at this questionand this questionabout accessing files inside jars and they both recommend temporarily extracting the file. I don't want to do that though.
采纳答案by skaffman
if it's inside a JAR, it's not a File, generally speaking. You should load the data using Class.getResourceAsStream(String)
, or something similar.
一般来说,如果它在 JAR 中,则它不是文件。您应该使用Class.getResourceAsStream(String)
或类似方法加载数据。
回答by AhmetB - Google
I think it is solved just here:
我认为它在这里解决了:
回答by Billy
If I remember correctly, getResourceAsStream() can behave differently depending on which web server your webapp is deployed, for instance I think it can be a problem when deployed as a war on a Websphere instance. But I'm not sure if this applies to you.
如果我没记错的话, getResourceAsStream() 的行为会因您的 web 应用程序部署的 Web 服务器而异,例如,我认为在 Websphere 实例上部署为战争时可能会出现问题。但我不确定这是否适用于你。
But I'm not sure you're trying to solve the "proper" problem : if it's a config file, that means is data dependant right ? Not code dependant ( your jar ) ? When the flat file will change, your config file will need to change as well, right ? If this is true, it sounds like the config should be better stored elsewhere, or even passed as a parameter to your jar.
但我不确定您是否正在尝试解决“正确”的问题:如果它是一个配置文件,那意味着数据依赖对吗?不依赖于代码(你的 jar)?当平面文件更改时,您的配置文件也需要更改,对吗?如果这是真的,听起来配置应该更好地存储在其他地方,或者甚至作为参数传递给您的 jar。
But maybe I haven't fully understood your problem...
但也许我还没有完全理解你的问题......
回答by user115086
Use Apache Commons Configuration, then you can read/write XML, auto update, find config file in path or jar, without a lot of hassles.
使用 Apache Commons Configuration,然后你就可以读/写 XML,自动更新,在路径或 jar 中查找配置文件,没有很多麻烦。