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

Thanks,
Menlo

Recommended Answers

All 10 Replies

Member Avatar for iamthwee

Why would you need to.

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.

Member Avatar for iamthwee

Well can't you use java's file I.O?

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?

Member Avatar for iamthwee

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.

Member Avatar for iamthwee

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.

Member Avatar for iamthwee

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.