Get path where the jar file was run

0

Hello, I am developing an application, which will be executed from a USB, but as the absolute path of a USB can vary depending on the equipment, whether it is in D:\ or F:\ ect, ect. I need to know how to get the location where the jar was executed.

at the beginning I put it in the following way

File sample_file = new File ("D: \ example.txt");

but you can imagine what happened when I run it on another computer. Any solution?

    
asked by Brian Hernandez 15.06.2018 в 06:47
source

1 answer

2
new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getPath();

With this delay you get the path of your jar (including the jar itself). You delete the jar and you already have the folder.

NOTE: Replace MyClass with the name of your class.

Source

    
answered by 15.06.2018 / 08:02
source