Access the file from the JAR in the same folder

I need to get (create and read) a file from a JAR file (executable jar), and this file must be created in the same directory as the JAR

I tried    this.getClass().getResource("myFile")  but since there are packages in the bank, this will not work.

I also tried to write File f = new File("myFile"); f.createNewFile();

and this works if I run the JAR from the terminal, but if I run the JAR by double-clicking it, the file will be created in my home directory -.- ''

how can I access the file, SURE this file is in the SAME directory as a JAR file?

(of course, also getting the absolute jar path would do the trick, since I can get the parent folder from it)

+5
source share
1

Jar:

String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

EDIT: , javascript , :). , :

String path = Me.class.getProtectionDomain().getCodeSource().getLocation().getPath();

( Me).

+7

All Articles