473,545 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12357
The following will get you a string that contains the full path
to the users working directory

String wd = System.getPrope rty("user.dir") ;

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

String home = System.getPrope rty("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.getPrope rty("file.separ ator");

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*@thresholdd igital.com> wrote in message
news:wn******** ***********@rwc rnsc51.ops.asp. att.net...
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.getPrope rty("user.dir") ;

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

String home = System.getPrope rty("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.getPrope rty("file.separ ator");

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*@thresholdd igital.com> wrote in message
news:wn******** ***********@rwc rnsc51.ops.asp. att.net...
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 "preference s" 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.use rNodeForPackage (Myclass.class) ;
Preferences sysprefs = Preferences.sys temNodeForPacka ge(Myclass.clas s);

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.getIn t("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.putIn t("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*@thresholdd igital.com> wrote in message
news:L6udb.6037 24$YN5.446895@s ccrnsc01...
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.getPrope rty("user.dir") ;

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

String home = System.getPrope rty("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.getPrope rty("file.separ ator");

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*@thresholdd igital.com> wrote in message
news:wn******** ***********@rwc rnsc51.ops.asp. att.net...
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 "preference s" 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.use rNodeForPackage (Myclass.class) ;
Preferences sysprefs = Preferences.sys temNodeForPacka ge(Myclass.clas s);
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.getIn t("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.putIn t("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*@thresholdd igital.com> wrote in message
news:L6udb.6037 24$YN5.446895@s ccrnsc01...
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.getPrope rty("user.dir") ;
>
> The following will get you a string that contains the full path
> to the users home directory
>
> String home = System.getPrope rty("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.getPrope rty("file.separ ator");
>
> 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*@thresholdd igital.com> wrote in message
> news:wn******** ***********@rwc rnsc51.ops.asp. att.net...
>> 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
3306
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 instance!
1
1870
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 to reuse the BaseHTTPserver classes. I am interested in finding a way to serve the dict without loading the whole dict into memory everytime an...
39
2614
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 ("file_name"). I looked at sscanf(), but that didn't seem to do what I wanted. Thanks, Joe
3
1254
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 the tab title. This is should be really basic, but I don't know of any other way (simple or complicated) of finding the path to a file. And as...
5
3399
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to...
3
1423
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 core files in the subdir also. could someone help me on how can i restrict my code to search the file in the current dir only import os, os.path...
3
3743
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 thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out....
1
3860
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 <javapgm>.class. Error message is: Warning: java.lang.ClassNotFoundException: javapgm Versions of software: Apache 2.0.59(Win32) PHP 4.4.4 Java ...
5
2142
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 /resources image1
0
7420
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7778
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.