• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Directory Location of Your Currently Running Java Application

 
Javini Javono
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In another thread, someone mentioned that if they were _not_
taking the exam, they would like to place the user preferences
in the same directory as the running Java application.
Besides a brute force search on the hard drive or floppy drive,
I didn't know that there was a System.getProperty(String) call
which would return this information. Is there one; and, if not,
how would you determine in what directory your application is
running?
Thanks,
Javini Javono
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yse the user.dir property gives you the running directory.
Mark
 
Javini Javono
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think my question must have been ill-formed
and confusing. Sorry.
The docs say:
user.dir is
User's current working directory

So, this would not, I don't believe, tell you the
directory of the placement of the .jar file which
contains the running application, but only the
directory of the user from which the user entered
the command.
If the user is in the directory
/Users/mrbean/tmp/
and issues the command
java -jar [path and filename.jar]
then when the application within this .jar file ran
and asked for System.getProperty("user.dir");
it would return
/Users/mrbean/tmp/ (or something like this)
but not the actual path and filename.jar where
the application is located on disk.
At least, that is how I understand it.
If my understanding is correct, then I still
don't understand how a running application
determines which directory it resides on
on the physical hard drive, or if this
is even possible to know besides a search
of the hard drive.
Thanks,
Javini Javono
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Javini,
Take a look at the code Phil posted in this thread - in that he has sample code for locating what directory the jar file is in.
But also note that the instructions for the new assignments are explicit - the properties file must be in the current working directory. Since it is a must requirement you risk automatic failure if you do not follow this.
Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic