This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to know the path of the application being executed  RSS feed

 
Fernando Dominguez
Ranch Hand
Posts: 108
Debian Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

It is posible to know the path of the application being executed?

Thanks in advanced.
 
Muhammad Ahsan Jamshaid
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this way...

public static void main(String[] args) {

File fl = new File("");
System.out.println("the Absolute Path.."+fl.getAbsolutePath());

}

--Jamshaid..
 
Fernando Dominguez
Ranch Hand
Posts: 108
Debian Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many Thanks
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Muhammad Ahsan Jamshaid:
Try this way...
public static void main(String[] args) {

File fl = new File("");
System.out.println("the Absolute Path.."+fl.getAbsolutePath());

}


Is using an empty string as an argument to File constructor portable or documented, or is it just a hack that you found happens to work? I'm guessing it's the latter, but am prepared to eat humble pie if you point me to the documentation. If it is just a hack, it should not be used, as there is a documented portable way to get the working directory.

The documented way of getting the current directory was from the system property "user.dir". As in, System.getProperty("user.dir") . I believe that returns an absolute path (it certainly does on Windows). To be sure, you might want to do new File(System.getProperty("user.dir")).getAbsolutePath().

General point: beware of replies that start "Try...". This tends to indicate the writer found it out by randomly experimenting, rather than by reading the documentation, to find the proper way.
[ September 27, 2006: Message edited by: Peter Chase ]
 
Muhammad Ahsan Jamshaid
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter...
here is the documetation reference..

public File(String pathname)

Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.

and getAbsolutePath() says

If this abstract pathname is the empty abstract pathname then the pathname string of the current user directory, which is named by the system property user.dir, is returned. Otherwise this pathname is resolved in a system-dependent way. On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory. On Win32 systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory.

I believe this works.... but your point is quit vaild we must use the system property to be more absolute..

--Jamshaid..
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I have been corrected about using new File(""). As it is documented in the API, it is OK to use it. Using the system property is still preferable, I reckon.
 
Muhammad Ahsan Jamshaid
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File("") was earlier reported as bug but was closed by commenting will not be fixed on SDN. here is the link for it.

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4107720

--Jamshiad..
 
Fernando Dominguez
Ranch Hand
Posts: 108
Debian Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!