如果我有一个需要文件路径的构造函数,我怎么能“伪造"?那如果是打包成罐子呢? [英] If I have a constructor that requires a path to a file, how can I "fake" that if it is packaged into a jar?

查看:17
本文介绍了如果我有一个需要文件路径的构造函数,我怎么能“伪造"?那如果是打包成罐子呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的上下文是,我试图在我编写的 Pig 脚本中使用 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.

这可能吗?

推荐答案

这对我有用.

假设您有一个包含 GeoLiteCity.dat 的包 org.foo.bar.util

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆