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

CompEng
  • 7,161
  • 16
  • 68
  • 122
  • `locate yourfile.jar` or `dir c:\yourfile.jar /s /a /p`? – Marc B Sep 10 '12 at 16:29
  • 3
    Exact Duplicate: http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file Reported for closing. – Arash Shahkar Sep 10 '12 at 16:30
  • no duplicate , I see it but it doesnt I want. I use this code : System.getProperty("user.dir") it works in windows but not work in unix – CompEng Sep 11 '12 at 07:30

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,405
  • 8
  • 54
  • 87
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,"");
CompEng
  • 7,161
  • 16
  • 68
  • 122