Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Myclass.class.getProtectionDomain().getCodeSource() return empty path

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?

like image 275
IdoS Avatar asked Aug 17 '15 12:08

IdoS


1 Answers

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).

like image 178
Roshana Pitigala Avatar answered Oct 29 '22 11:10

Roshana Pitigala