How can I find out where the class files are installed in a programmtic way?

Thanks,
Menlo

We create some logs and other meta data files for use during the duration of the java app. We would like to make sure they are created and looked for in the same place everytime no matter what the file structure of the users computer looks like. So, we would like to create these files near the class files since that shouldn't change after the app is installed.

Yes I can create the files just fine. I would like make sure they are created in the right plaec, i.e. where the app is installed. How do I figure out where it is installed?

Well when you create it.

I.e create file ("c:\\programs files\\myapp.jar")

it will be c:\\programs files\\myapp.jar <- there?

Not sure about other OS though?

No it won't be .. when the user installs the app they are allowed to install it anywhere they want. So there is no guarantee where the jar file will end up when the app is started and the files need to be created.

Well then you're screwed. Unless you wanna recursively search all the drives on the machine for your app and its path?

I have a hard time believing that .... something somewhere has to know where the code is.

Well, a stab in the dark might be the registry?

I mean if your apps records the path the user choses to install it to and then writes it to the registry. In short, that's what it boils down to. It must record the path and store it somewhere that will ALWAYS BE IN THE SAME place. Otherwise it's the long winded method.

The system classloader hides all that from you.
There is no way to find out the filesystem location of a classfile, only the classloader that loaded it.
And you wouldn't need to anyway.
Create your installer in such a way that it writes a configuration file containing the desired location of the logfile, and use that to determine where the log is to be written.

Or use the registry (more correctly the configuration API, which determines based on OS capabilities where to store settings).

This article has been dead for over six months. Start a new discussion instead.