0

Possible Duplicates:
Where on the file system was my java class loaded from?
How do I get the directory that the currently executing jar file is in?

How can i know .jar file directory within my program i want to use this directory to create another files in same directory which user save this .jar file

Community
  • 1
  • 1
Moataz Aahmed Mohammed
  • 1,307
  • 5
  • 22
  • 33
  • 1
    This thread seems closely related to your earlier question (http://stackoverflow.com/questions/5052311/how-can-my-java-program-store-files-inside-of-its-jar-file-and-use-it-for-read-a) in which 2 people suggested a sub-directory of user.home to store the file in. We recommended that for good reason, if you do not understand why, it is probably best to take it up on the original thread. And while I am here, you are also still focusing on the strategy, rather than the goal. ;) – Andrew Thompson Feb 19 '11 at 20:15

1 Answers1

1

I don't think you can easily find out where the JAR file is, but you can find out which directory your program is being run from:

File cwdFile = new File (".");
String cwd = cwdFile.getAbsolutePath();

This only works if the user actually runs the JAR file from the directory it's in:

java -jar MyExectuableJar.jar

If they run it from another directory, like this:

java -jar /usr/bin/java/MyExecutableJar.jar

then you'll get whichever directory the user happens to be in at the time.

Stewart Murrie
  • 1,319
  • 7
  • 12
  • You don't need to know the absolutePath to do something with `new File (".")`. But as you said yourself: The current working dir has nothing to do with the place where the jar is - only incidental. – user unknown Feb 19 '11 at 20:21