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

1
  • 1
    This thread seems closely related to your earlier question (stackoverflow.com/questions/5052311/…) 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. ;) Commented Feb 19, 2011 at 20:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.