By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,811 Members | 785 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,811 IT Pros & Developers. It's quick & easy.

Class File Finding It's Own Path?

P: n/a
I want to have a config file for my program, which means I need to know
where the config file is. If I type:

java myclass

and it runs myclass.class, is there any way to obtain the location of the
file myclass.class? Will this work if it's run from a relative path, like:

java ../progs/myclass

or from other directories? I figure I won't always know for sure the
program someone installs this program in, so I want to be able to always
find the config file.

How about if a class file on a Windows box is started by double clicking
from the GUI? (Or Linux or any *nix, for that matter...)

Right now I'm thinking I'll have a batch file and ALWAYS run it from the
batch file and have it run from a command line like:

java /absolute/path/to/program/myclass "--home=/absolute/path/to/program"

and that way I can parse the argument. I have yet to find a way to parse
the command line. (The installer util I found can parse files it installs,
so it can make sure the home path is inserted in the batch file where
needed.)

I'm sure this is a common problem (a class needing to find config files), so
how do most people make sure a Java class can find a "home" directory or
config/setting files?

Thanks!

Hal
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
The following will get you a string that contains the full path
to the users working directory

String wd = System.getProperty("user.dir");

The following will get you a string that contains the full path
to the users home directory

String home = System.getProperty("user.home");

It is customary to put the config file in the users home directory.
Also, don't forget to use the platform independent separator
character which is "\" for windows and "/" for linux, unix
Here is how to get the separator

String fs = System.getProperty("file.separator");

Or you can create a File object like so

File configfile = new File(home, ".config");

where home is the string from above and ".config" is
the name of the config file you want to use

HTH, phil

"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:wn*******************@rwcrnsc51.ops.asp.att.n et...
I want to have a config file for my program, which means I need to know
where the config file is. If I type:

java myclass

and it runs myclass.class, is there any way to obtain the location of the
file myclass.class? Will this work if it's run from a relative path, like:
java ../progs/myclass

or from other directories? I figure I won't always know for sure the
program someone installs this program in, so I want to be able to always
find the config file.

How about if a class file on a Windows box is started by double clicking
from the GUI? (Or Linux or any *nix, for that matter...)

Right now I'm thinking I'll have a batch file and ALWAYS run it from the
batch file and have it run from a command line like:

java /absolute/path/to/program/myclass "--home=/absolute/path/to/program"

and that way I can parse the argument. I have yet to find a way to parse
the command line. (The installer util I found can parse files it installs, so it can make sure the home path is inserted in the batch file where
needed.)

I'm sure this is a common problem (a class needing to find config files), so how do most people make sure a Java class can find a "home" directory or
config/setting files?

Thanks!

Hal

Jul 17 '05 #2

P: n/a
Phil... wrote:

It helps quite a bit!
The following will get you a string that contains the full path
to the users working directory

String wd = System.getProperty("user.dir");

The following will get you a string that contains the full path
to the users home directory

String home = System.getProperty("user.home");

It is customary to put the config file in the users home directory.
Is there a way to find out where the program/class resides for config info
that will remain the same for all users but might change from time to time
(like DNS servers that could change with their ISP or the mail server
address)?
Also, don't forget to use the platform independent separator
character which is "\" for windows and "/" for linux, unix
Here is how to get the separator
I knew I could get the file separator from a File object, but didn't know I
could get it that way.

Is there a way to get a list of all the properties I can get this way?
String fs = System.getProperty("file.separator");

Or you can create a File object like so

File configfile = new File(home, ".config");

where home is the string from above and ".config" is
the name of the config file you want to use

HTH, phil
Thanks!

Hal
"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:wn*******************@rwcrnsc51.ops.asp.att.n et...
I want to have a config file for my program, which means I need to know
where the config file is. If I type:

java myclass

and it runs myclass.class, is there any way to obtain the location of the
file myclass.class? Will this work if it's run from a relative path,

like:

java ../progs/myclass

or from other directories? I figure I won't always know for sure the
program someone installs this program in, so I want to be able to always
find the config file.

How about if a class file on a Windows box is started by double clicking
from the GUI? (Or Linux or any *nix, for that matter...)

Right now I'm thinking I'll have a batch file and ALWAYS run it from the
batch file and have it run from a command line like:

java /absolute/path/to/program/myclass "--home=/absolute/path/to/program"

and that way I can parse the argument. I have yet to find a way to parse
the command line. (The installer util I found can parse files it

installs,
so it can make sure the home path is inserted in the batch file where
needed.)

I'm sure this is a common problem (a class needing to find config files),

so
how do most people make sure a Java class can find a "home" directory or
config/setting files?

Thanks!

Hal


Jul 17 '05 #3

P: n/a
I don't know how to find out where the program/class resides.

If you want to put some stuff in a place where all users can get to it
look at the "preferences" stuf in java.util.prefs. You need java version 1.4
This package contains classes and interfaces for managing persistent
user and system-wide preferences for Java applications and classes.
There is a "system" and a "user" preferences. I played with it to see
what happens. I used

Preferences userprefs = Preferences.userNodeForPackage(Myclass.class);
Preferences sysprefs = Preferences.systemNodeForPackage(Myclass.class);

The user one is stored in a different place for each user, but the
system one is in the same place for all users so I think you want to use
system.

I made a separate program to set the values since I expected the user
to only read the values like this

int width = userprefs.getInt("width", 80);
int width = sysprefs.getInt("width", 80);

where "width" is the string that represents the item and you are
supplying a default value of 80 in case it is not found. If "width"
is not found you get 80 back so no errors are generated.

set a value in the preferences like this

userprefs.putInt("width", 72);

On my windows XT machine it put the stuff in the registry using a
very simple XML syntax. It is supposed
to use some directory structure on linux, unix machines for this.

HTH, Phil...
"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:L6udb.603724$YN5.446895@sccrnsc01...
Phil... wrote:

It helps quite a bit!
The following will get you a string that contains the full path
to the users working directory

String wd = System.getProperty("user.dir");

The following will get you a string that contains the full path
to the users home directory

String home = System.getProperty("user.home");

It is customary to put the config file in the users home directory.
Is there a way to find out where the program/class resides for config info
that will remain the same for all users but might change from time to time
(like DNS servers that could change with their ISP or the mail server
address)?
Also, don't forget to use the platform independent separator
character which is "\" for windows and "/" for linux, unix
Here is how to get the separator


I knew I could get the file separator from a File object, but didn't know

I could get it that way.

Is there a way to get a list of all the properties I can get this way?
String fs = System.getProperty("file.separator");

Or you can create a File object like so

File configfile = new File(home, ".config");

where home is the string from above and ".config" is
the name of the config file you want to use

HTH, phil


Thanks!

Hal
"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:wn*******************@rwcrnsc51.ops.asp.att.n et...
I want to have a config file for my program, which means I need to know
where the config file is. If I type:

java myclass

and it runs myclass.class, is there any way to obtain the location of the file myclass.class? Will this work if it's run from a relative path,

like:

java ../progs/myclass

or from other directories? I figure I won't always know for sure the
program someone installs this program in, so I want to be able to always find the config file.

How about if a class file on a Windows box is started by double clicking from the GUI? (Or Linux or any *nix, for that matter...)

Right now I'm thinking I'll have a batch file and ALWAYS run it from the batch file and have it run from a command line like:

java /absolute/path/to/program/myclass "--home=/absolute/path/to/program"
and that way I can parse the argument. I have yet to find a way to parse the command line. (The installer util I found can parse files it

installs,
so it can make sure the home path is inserted in the batch file where
needed.)

I'm sure this is a common problem (a class needing to find config files),
so
how do most people make sure a Java class can find a "home" directory

or config/setting files?

Thanks!

Hal

Jul 17 '05 #4

P: n/a
Phil... wrote:
I don't know how to find out where the program/class resides.

If you want to put some stuff in a place where all users can get to it
look at the "preferences" stuf in java.util.prefs. You need java version
1.4 This package contains classes and interfaces for managing persistent
user and system-wide preferences for Java applications and classes.
There is a "system" and a "user" preferences. I played with it to see
what happens. I used

Preferences userprefs = Preferences.userNodeForPackage(Myclass.class);
Preferences sysprefs = Preferences.systemNodeForPackage(Myclass.class);
That is fantastic! It is exactly what I need and I would have NEVER found
it on my own. I am using 1.4 (1.4.1, I think), so that'll be exactly what
I need. I'm using a number of files, each one line long and storing one
value (the key is the filename with ".cnf" added). This seems to be the
easiest way for me to store the different "modes" and settings, since it is
possible for a user to change them from a control panel while running and
it works much better than storing them all in one file. With this system I
can bypass the .conf file all together and just use the Preferences stuff.
I'll check it out and see what it offers.
The user one is stored in a different place for each user, but the
system one is in the same place for all users so I think you want to use
system.

I made a separate program to set the values since I expected the user
to only read the values like this

int width = userprefs.getInt("width", 80);
int width = sysprefs.getInt("width", 80);

where "width" is the string that represents the item and you are
supplying a default value of 80 in case it is not found. If "width"
is not found you get 80 back so no errors are generated.

set a value in the preferences like this

userprefs.putInt("width", 72);

On my windows XT machine it put the stuff in the registry using a
very simple XML syntax. It is supposed
to use some directory structure on linux, unix machines for this.

HTH, Phil...
It helps a lot -- it's one of those little things you run into once in a
while that makes part of a program so simple it's actually worth getting
excited about! Thanks!

Hal

"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:L6udb.603724$YN5.446895@sccrnsc01...
Phil... wrote:

It helps quite a bit!
> The following will get you a string that contains the full path
> to the users working directory
>
> String wd = System.getProperty("user.dir");
>
> The following will get you a string that contains the full path
> to the users home directory
>
> String home = System.getProperty("user.home");
>
> It is customary to put the config file in the users home directory.


Is there a way to find out where the program/class resides for config
info that will remain the same for all users but might change from time
to time (like DNS servers that could change with their ISP or the mail
server address)?
> Also, don't forget to use the platform independent separator
> character which is "\" for windows and "/" for linux, unix
> Here is how to get the separator


I knew I could get the file separator from a File object, but didn't know

I
could get it that way.

Is there a way to get a list of all the properties I can get this way?
> String fs = System.getProperty("file.separator");
>
> Or you can create a File object like so
>
> File configfile = new File(home, ".config");
>
> where home is the string from above and ".config" is
> the name of the config file you want to use
>
> HTH, phil


Thanks!

Hal
> "Hal Vaughan" <ha*@thresholddigital.com> wrote in message
> news:wn*******************@rwcrnsc51.ops.asp.att.n et...
>> I want to have a config file for my program, which means I need to
>> know
>> where the config file is. If I type:
>>
>> java myclass
>>
>> and it runs myclass.class, is there any way to obtain the location of the >> file myclass.class? Will this work if it's run from a relative path,
> like:
>>
>> java ../progs/myclass
>>
>> or from other directories? I figure I won't always know for sure the
>> program someone installs this program in, so I want to be able to always >> find the config file.
>>
>> How about if a class file on a Windows box is started by double clicking >> from the GUI? (Or Linux or any *nix, for that matter...)
>>
>> Right now I'm thinking I'll have a batch file and ALWAYS run it from the >> batch file and have it run from a command line like:
>>
>> java /absolute/path/to/program/myclass "--home=/absolute/path/to/program" >>
>> and that way I can parse the argument. I have yet to find a way to parse >> the command line. (The installer util I found can parse files it
> installs,
>> so it can make sure the home path is inserted in the batch file where
>> needed.)
>>
>> I'm sure this is a common problem (a class needing to find config files), > so
>> how do most people make sure a Java class can find a "home" directory or >> config/setting files?
>>
>> Thanks!
>>
>> Hal


Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.