如果我有一个需要文件路径的构造函数,我该如何“伪造”如果它被包装成一个罐子? [英] If I have a constructor that requires a path to a file, how can I "fake" that if it is packaged into a jar?
问题描述
这个问题的上下文是,我正尝试在我编写的猪脚本中使用maxmind java api ...但我不认为知道这两者是否有必要回答这个问题,但是。
The context of this question is that I am trying to use the maxmind java api in a pig script that I have written... I do not think that knowing about either is necessary to answer the question, however.
maxmind API有一个构造函数,它需要一个名为GeoIP.dat的文件的路径,该文件是一个逗号分隔的文件,它具有所需的信息。
The maxmind API has a constructor which requires a path to a file called GeoIP.dat, which is a comma delimited file which has the information it needs.
我有一个包含API的jar文件,以及一个实例化类并使用它的包装类。我的想法是将GeoIP.dat文件打包到jar文件中,然后将其作为jar文件中的资源进行访问。问题是,我不知道如何构造构造函数可以使用的路径。
I have a jar file which contains the API, as well as a wrapping class which instantiates the class and uses it. My idea is to package the GeoIP.dat file into the jar, and then access it as a resource in the jar file. The issue is that I do not know how to construct a path that the constructor can use.
看看API,它们是如何加载文件的:
Looking at the API, this is how they load the file:
public LookupService(String databaseFile) throws IOException {
this(new File(databaseFile));
}
public LookupService(File databaseFile) throws IOException {
this.databaseFile = databaseFile;
this.file = new RandomAccessFile(databaseFile, "r");
init();
}
我只粘贴它,因为我不反对编辑API本身这项工作,如果有必要的话,但不知道如何我可以复制我这样的功能。理想情况下,我希望将它放入文件表单中,否则,编辑API将非常麻烦。
I only paste that because I am not averse to editing the API itself to make this work, if necessary, but do not know how I could replicate the functionality I as such. Ideally I'd like to get it into the file form, though, or else editing the API will be quite a chore.
这可能吗?
推荐答案
这适用于我。
假设你有一个包org.foo.bar .util包含GeoLiteCity.dat
Assuming you have a package org.foo.bar.util that contains GeoLiteCity.dat
URL fileURL = this.getClass().getResource("org/foo/bar/util/GeoLiteCity.dat");
File geoIPData = new File(fileURL.toURI());
LookupService cl = new LookupService(geoIPData, LookupService.GEOIP_MEMORY_CACHE );
这篇关于如果我有一个需要文件路径的构造函数,我该如何“伪造”如果它被包装成一个罐子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!