473,413 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Class File Finding It's Own Path?

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
4 12347
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this...
1
by: Yin | last post by:
Hello. I am using the basehttpserver to implement the HTTP protocol to serve a fairly large lexicon that I have loaded as a dictionary in python. Rather than writing a whole server, I would like...
39
by: Joe Laughlin | last post by:
If I have a character array with "/some/file/directory/file_name", are there any functions / libraries that I could use to separate the directory ("some/file/directory") from the file name...
3
by: Javaman59 | last post by:
Visual Studio 2003/5 question... I'm in the solution explorer, looking at a file. I want to find it's path. The only way i know of doing this is to open it (with a double-click), then mouse over...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: su | last post by:
Hi , I am trying to understand myself with some basic programs in python, I have written a small script to search for core files in the current dir. but when i tried to execute, it is searching the...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
1
by: aapexclient | last post by:
I have a problem with Java finding my custom class directory. I have searched the forums and tried everything, but nothing seems to work. The <javapgm>.java compiled cleanly and created...
5
by: ron.longo | last post by:
Is there any way that I can find the path of the main .py file of my application? For example, I have an application with some resources which are in a subdirectory: myPythonApp.py...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.