Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Application Path

Thread view: 
Roman - 17 Dec 2005 13:53 GMT
Hello,

could someone pleas tell me, how I find out from inside the program, in
what path it is installed?

The property user.dir returns only the working directory but this is not
necessary the path to the Application.

System.getenv(String) is deprecated.

Thank you for any answers in advance.

Signature

Gruesse Roman

Jean-Francois Briere - 17 Dec 2005 22:10 GMT
Try:
System.getProperty("java.home");
(then you could append bin/java -or- bin\java.exe -or- bin\javaw.exe)

Regards
Andrew Thompson - 17 Dec 2005 23:29 GMT
> could someone pleas tell me, how I find out from inside the program, in
> what path it is installed?

<http://www.physci.org/codes/javafaq.jsp#path>

Signature

Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew

Roedy Green - 18 Dec 2005 01:06 GMT
>could someone pleas tell me, how I find out from inside the program, in
>what path it is installed?

There are several different meanings to that question. One of them is
where did a given class file come from. See
http://mindprod.com/jgloss/where.html
Signature

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

Roman - 18 Dec 2005 09:46 GMT
Thanks to all who answered. I got it.

Signature

Gruesse Roman

David Segall - 18 Dec 2005 14:19 GMT
>Thanks to all who answered. I got it.
Could you post the code please?
Roman - 18 Dec 2005 16:24 GMT
>>Thanks to all who answered. I got it.
>
> Could you post the code please?

Here a stand-alone example:

import java.net.URL;

public class App {
    public static void main(String[] args) {
        App app = new App();
        System.out.println(
            (app.getClass().getResource("App.class")).getPath()
            );
    }
}   

   
Signature

Gruesse Roman

Roman - 18 Dec 2005 16:43 GMT
>>> Thanks to all who answered. I got it.
>>
[quoted text clipped - 14 lines]
>
>    

Of course you could replace "App.class" by

app.getClass().getName() + ".class"

Signature

Gruesse Roman

Chris Smith - 18 Dec 2005 18:26 GMT
> Here a stand-alone example:
>
[quoted text clipped - 8 lines]
>      }
> }   

This code doesn't work.  Or rather, it only works if your application is
composed of loose class files, but obviously isn't (or, at least,
hopefully isn't!) the case when you deploy the application.

I wrote a very complete and detailed answer to this question some time
ago.  It's temporarily available in Word format at the following rather
ugly URL:

> http://www.javamoderator.org/Java%20White%20Papers/05%20%2D%20Miscellaneous/Find
ing%20Application%20Intrinsic%20Data.doc

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 19 Dec 2005 01:21 GMT
>> http://www.javamoderator.org/Java%20White%20Papers/05%20%2D%20Miscellaneous/Find
ing%20Application%20Intrinsic%20Data.doc

IF you want I could format that as HTML and with an attribution add it
as http://mindprod.com/jgloss/intrinsicdata.html
Signature

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

Chris Smith - 19 Dec 2005 04:26 GMT
> IF you want I could format that as HTML and with an attribution add it
> as http://mindprod.com/jgloss/intrinsicdata.html

In this case I have to say no.  I wrote that on my employer's time, and
our plan was to collect a lot of similar documents, convert them to PDF
in a similar format, and place them on MindIQ's web site to lure Java
programmers' search engines in hopes that they'd see our commercial
training courses.  That plan isn't actually progressing, but I can't
just give away content that technically belongs to MindIQ.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Jean-Francois Briere - 19 Dec 2005 08:21 GMT
>> app.getClass().getResource("App.class")

> This code doesn't work.  Or rather, it only works if your application is
> composed of loose class files

Actually the code works for either loose class files or class files
within jar files.

Regards
David Segall - 19 Dec 2005 15:12 GMT
>> Here a stand-alone example:
>>
[quoted text clipped - 17 lines]
>
>> http://www.javamoderator.org/Java%20White%20Papers/05%20%2D%20Miscellaneous/Find
ing%20Application%20Intrinsic%20Data.doc
Thank you for the code in technique #5 which returns the appropriately
formatted directory name that I would expect.

Could you explain why you say using .getResource() does not work? The
raw result seems similar to that obtained from
getProtectionDomain().getCodeSource() and I don't see why some similar
post-processing would not yield the same directory name.
Chris Smith - 19 Dec 2005 16:07 GMT
> Could you explain why you say using .getResource() does not work? The
> raw result seems similar to that obtained from
> getProtectionDomain().getCodeSource() and I don't see why some similar
> post-processing would not yield the same directory name.

Well, for one thing, getResource() is not guaranteed to work on a class
file in the first place.  But in practice, it does work; so that's not
the most important problem.

The real problem is that the original code just doesn't work for code
that's packaged in a JAR file.  getResource() returns a URL with the
"jar" scheme, and calling "getPath" on that URL doesn't do what you
want.  getCodeSource(), on the other hand, will return a file-scheme URL
pointing to the JAR file itself.  That is what you want.

getCodeSource() is also more convenient even for loose classes, if they
are in packages.  getResource() returns a path that points deep into the
package structure.  getCodeSource() points to the root of that
structure, which is almost certainly what you wanted in the first place.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

David Segall - 19 Dec 2005 16:40 GMT
>> Could you explain why you say using .getResource() does not work? The
>> raw result seems similar to that obtained from
[quoted text clipped - 15 lines]
>package structure.  getCodeSource() points to the root of that
>structure, which is almost certainly what you wanted in the first place.
Thanks Chris. I'm convinced. I think it would be worthwhile to add
this explanation to your essay since getResource() is most frequently
proposed as the solution to the problem.
David Segall - 19 Dec 2005 06:52 GMT
>>>Thanks to all who answered. I got it.
>>
[quoted text clipped - 14 lines]
>
>   
Thanks Roman. On my system that returns
"file:/C:/Program%20Files/App/App.jar!/App.class". I was hoping for
something that needed less post-processing to yield
"C:Program Files/App".
ricky.clarkson@gmail.com - 19 Dec 2005 01:03 GMT
No, System.getenv(String) is not deprecated.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getenv()

Compare and contrast with
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv()
Roedy Green - 19 Dec 2005 02:11 GMT
On 18 Dec 2005 17:03:26 -0800, "ricky.clarkson@gmail.com"
<ricky.clarkson@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>No, System.getenv(String) is not deprecated.

It was deprecated for a while, then made respectable again.
Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.