1

I have a.jar file. I want to its path where ever it is when it running.

I use this code : System.getProperty("user.dir") it works in windows but not work in unix

Ersin Gülbahar
  • 6,713
  • 15
  • 60
  • 114

2 Answers2

0

If you really really need this, here's a solution:

MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();

It is, however, far from pretty ;). Consider using other mechanisms. (Source: a slideset of mine)

Miquel
  • 15,055
  • 8
  • 52
  • 86
0

the answer is :

in windows :

String yourJarName="yourJarName.jar"; 
String Jar_path= MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath().replace(yourJarName,"").substring(1);

in unix :

   String yourJarName="yourJarName.jar"; 
    String Jar_path= MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath().replace(yourJarName,"");
Ersin Gülbahar
  • 6,713
  • 15
  • 60
  • 114