hi,
can someone show me a snippet of java code to return at runtime, the
complete directory path from where a application is launched.
suppose my java application is stored and launched from:
D:\myFolder\myApplication.exe
thanks in advance,
Sachin.
NullBock - 16 Feb 2006 11:45 GMT
Howdy,
I just quickly stripped this down, so I don't know if it's totally
bullet-proof...
public class Main {
public static String getLocation() throws Exception {
String sName = '/' + Main.class.getName().replace('.', '/') +
".class";
System.out.println(sName);
System.out.println(Main.class.getResource(sName));
String s = Main.class.getResource(sName).getFile();
int start = s.indexOf('/') + 1, end = s.lastIndexOf('!');
if (end < 0) {
s = s.substring(start, s.length() - sName.length());
} else {
s = s.substring(start, end);
}
return s;
}
}
It'll return the base-directory containing the Main /class-file/, which
isn't necessarily the location of your executable. (For instance, if
the executable is a shell that doesn't include the Main class in an
internal archive.)
If you need to find an executable that doesn't contain the classes,
then you've got your work cut out for you. Either you'd have to go
JNI, or do a scan of all the directories on the PATH, looking for the
executable. Of course, then you'd have to know in advance what the
executable was called...
Hope this helps,
Walter
----
Walter Gildersleeve
Freiburg, Germany
______________________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green.
NullBock - 16 Feb 2006 11:47 GMT
Sorry 'bout the printlines--that was for debugging...
Walter
----
Walter Gildersleeve
Freiburg, Germany
______________________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green.
Roedy Green - 16 Feb 2006 14:17 GMT
>can someone show me a snippet of java code to return at runtime, the
>complete directory path from where a application is launched.
>suppose my java application is stored and launched from:
>D:\myFolder\myApplication.exe
There are so many ways of launching, this has remained undefined.
jet xxx.exe
java class
java -.jar
java exe wrapper that gets a class or jar
kicker that launches a java.exe with parms
Yo ucan get the CWD. See http://mindprod.com/jgloss/file.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Stephen - 17 Feb 2006 10:29 GMT
You mean this;
System.getProperty("user.dir");
--
Regards;
Stephen
Chris Smith - 17 Feb 2006 15:11 GMT
> You mean this;
>
> System.getProperty("user.dir");
No, that definitely doesn't do what was asked. That returns the current
working directory... a concept that's next to meaningless except in a
command-line environment, and that therefore ought to be ignored any
time you don't know better.
Many IDEs will set the current working directory to the project
directory when running your code. That's very convenient for writing
simple test code. Unfortunately, if you rely on it for real code, your
program will probably break at deployment.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Roedy Green - 18 Feb 2006 00:47 GMT
>> System.getProperty("user.dir");
>
>No, that definitely doesn't do what was asked. That returns the current
>working directory...
user.dir MIGHT be the CWD, but not necessarily.
To get the CWD see http://mindprod.com/jgloss/cwd.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Daniel Dyer - 18 Feb 2006 01:17 GMT
>>> System.getProperty("user.dir");
>>
[quoted text clipped - 4 lines]
>
> To get the CWD see http://mindprod.com/jgloss/cwd.html
Under what circumstances does the property "user.dir" not refer to the
current working directory?
Dan.

Signature
Daniel Dyer
http://www.dandyer.co.uk
Daniel Dyer - 18 Feb 2006 01:20 GMT
> Under what circumstances does the property "user.dir" not refer to the
> current working directory?
To answer my own question, when the program has over-written that value
via the System.setProperty method. Is that what you are referring to,
Roedy, or is there something else?
Dan.

Signature
Daniel Dyer
http://www.dandyer.co.uk
Roedy Green - 18 Feb 2006 02:44 GMT
On Sat, 18 Feb 2006 01:17:35 -0000, "Daniel Dyer"
<dan@dannospamformepleasedyer.co.uk> wrote, quoted or indirectly
quoted someone who said :
>Under what circumstances does the property "user.dir" not refer to the
>current working directory?
run Wassup in various browsers.
http://mindprod.com/applets/wassup.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.