Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I have a constructor that requires a path to a file, how can I "fake" that if it is packaged into a jar?

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.

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.

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.

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();
}

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.

Is this possible?

like image 813
A Question Asker Avatar asked Dec 28 '22 01:12

A Question Asker


2 Answers

Try:

new File(MyWrappingClass.class.getResource(<resource>).toURI())
like image 159
Puce Avatar answered Jan 05 '23 15:01

Puce


dump your data to a temp file, and feed the temp file to it.

File tmpFile = File.createTempFile("XX", "dat");
tmpFile.deleteOnExit();

InputStream is = MyClass.class.getResourceAsStream("/path/in/jar/XX.dat");
OutputStream os = new FileOutputStream(tmpFile)

read from is, write to os, close
like image 26
irreputable Avatar answered Jan 05 '23 15:01

irreputable