jfabian
jfabian

Reputation: 11

Search File from current directory in a .jar Java

I want to know how I can search a File on the System, but the File will be place in the same directory that the .jar. Example:

MySearchProgram.jar (executable) MyFile.txt

Both files(MySearchProgram.jar, MyFile.txt) will be place in the same directory: /home/jfabian/Documents I need to read that file, to do that i need to Find it and specified the MyFile.txt directory, the problem is that I want to do it, from a .jar executable.

I know how to find a file, specifying the root, with this algorithm provide by Oracle: [Find.java][1]

Thanks you in advance

Upvotes: 1

Views: 168

Answers (1)

Ingo Kegel
Ingo Kegel

Reputation: 48105

To determine the directory where your JAR file file is located, use

Test.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()

on any class that was loaded from the JAR file (Test in the example above).

Upvotes: 1

Related Questions