i'm trying to find the location of the running jar file using the method:
File jarFile = new File(JarPath.class.getProtectionDomain().getCodeSource().getLocation().toURI());
when i run it on the IDE (eclipse) it returns the correct path. but when i run the jar as an executable the code source returned is
rsrc:./
ideas on how the get the correct path?
Try a different approach to get the location.
String jarFilePath = ClassLoader.getSystemClassLoader().getResource(".")
.toURI()
.getPath()
.replaceFirst("/", "");
This gives you up to the parent location of the jar file.
C:/Users/Roshana Pitigala/Desktop/
You still need to add the filename and the extension (jarFileName.jar
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With