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 / First Aid / August 2004

Tip: Looking for answers? Try searching our database.

Installation directory path

Thread view: 
Sveta - 29 Aug 2004 21:49 GMT
Hello everybody,

My application needs to keep reading files from the directory in which it
will be intsalled. How can I implement this in my code?

I don't think that something like System.getProperty("user.dir") is what I
am after, as the user may have a batch file and run the application from
wherever he/she likes. I don't want the directory the application is running
from currently, but the actual installation directory.  (By the way, this
should also be cross platform ).

Any ideas?

Thank you in advance,

Sveta
Paul Lutus - 29 Aug 2004 23:37 GMT
> Hello everybody,
>
[quoted text clipped - 9 lines]
>
> Any ideas?

public class AppDirLocation {
 
  static public void main(String[] args)
  {
     AppDirLocation app = new AppDirLocation();
     System.out.println(app.getClass().getProtectionDomain()
     .getCodeSource().getLocation());
  }
}

This can be done statically as well:

(class name).getProtectionDomain().getCodeSource().getLocation()

Signature

Paul Lutus
http://www.arachnoid.com

Andrew Thompson - 30 Aug 2004 01:42 GMT
> I don't think that something like System.getProperty("user.dir") is what I
> am after,

..right idea, wrong user directory, put the files in "user.home"..
<http://www.physci.org/codes/javafaq.jsp#path>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Paul Lutus - 30 Aug 2004 02:53 GMT
>> I don't think that something like System.getProperty("user.dir") is what
>> I am after,
>
> ..right idea, wrong user directory, put the files in "user.home"..
> <http://www.physci.org/codes/javafaq.jsp#path>

The OP actually wants the directory the running application is located in. I
know this isn't always possibe, but see my other post for one answer.

Signature

Paul Lutus
http://www.arachnoid.com

Sveta - 30 Aug 2004 08:49 GMT
Many thanks,

would (class name).getProtectionDomain().getCodeSource().getLocation() work
for jar as well?

Sveta
Paul Lutus - 30 Aug 2004 09:01 GMT
> Many thanks,
>
> would (class name).getProtectionDomain().getCodeSource().getLocation()
> work for jar as well?

Why don't you try it? Don't you have access to a Java compiler? Create the
example program, run it, satisfy yourself that it works, then jar it and
try again.

Signature

Paul Lutus
http://www.arachnoid.com

Sveta - 30 Aug 2004 11:28 GMT
OK, will also do this later. In the meanwhile I tried:

System.out.println("Loaded MyInterface from:" +
MyInterface.class.getProtectionDomain().getCodeSource().getLocation());

and it keeps coming up with null pointer exception messages.

Also tried
this.getClass().etc
and also this.getClass().getResource("MyInterface.class") all come up with
problems : java.lang.NullPointerException

Sveta
Andrew Thompson - 30 Aug 2004 13:18 GMT
> and also this.getClass().getResource("MyInterface.class") all come up with
> problems : java.lang.NullPointerException

If MyInterface.class is in package mypackage,
you need to reference it as..
this.getClass().getResource("mypackage/MyInterface.class");

But.. whay are you not importing this class?

Why are you trying to load it by name?

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Sveta - 30 Aug 2004 14:04 GMT
Paul/Andrew

thanks for the suggestions.

Actually, both these now work (haven't tried with jar yet):

ClassLoader.getSystemClassLoader().getSystemResource("MyInterface.class"));
and MyInterface.class.getProtectionDomain().getCodeSource().getLocation());

The probelm was possibly that the first time I run the application through
the GUI IDE which may have interferred?? (and yes the application was in a
package). Then I stopped using the IDE but in the meanwhile I managed to
insert a bug in the init() method !
(Sorry for the extra message due to my fault...)

The whole point of doing this was to be able to dynamically get the names of
the files from which info is read.
I have a version using an environmental variable but I preferred not to have
to set anything and make the program able to sort things out by itself.
And I had no clue how to do this (importing? not sure how).

Sveta

> > and also this.getClass().getResource("MyInterface.class") all come up with
> > problems : java.lang.NullPointerException
[quoted text clipped - 6 lines]
>
> Why are you trying to load it by name?
Paul Lutus - 30 Aug 2004 16:55 GMT
> OK, will also do this later. In the meanwhile I tried:
>
> System.out.println("Loaded MyInterface from:" +
> MyInterface.class.getProtectionDomain().getCodeSource().getLocation());
>
> and it keeps coming up with null pointer exception messages.

In order to get assistance, you must post a short, complete, compilable
example of the problem code.

Signature

Paul Lutus
http://www.arachnoid.com



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



©2008 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.