wood burning stoves 2.0*
The moose likes Java in General and the fly likes System.getProperty( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "System.getProperty( "user.home" ) is unreliable?" Watch "System.getProperty( "user.home" ) is unreliable?" New topic
Author

System.getProperty( "user.home" ) is unreliable?

Bill Compton
Ranch Hand

Joined: Aug 26, 2000
Posts: 186
We're building a desktop app that needs to store some files in a reliable location. Currently we call System.getProperty( "user.home" ) and append a directory from there. It's been almost perfectly reliable for over a year, returning C:\Documents and Settings\{my user id}. However twice it has returned something else. Once, I got back C:\WINDOWS\system32\config\systemprofile and once I got C:\Documents and Settings\LocalService. Both times, after rebooting, the return from System.getProperty( "user.home" ) went back to "normal."

Is System.getProperty( "user.home" ) the wrong way to get a reliable user-related directory (i.e. "home") location? If that's wrong, what's right?

If it matters, I'm on Windows XP Home running Java 1.6.0_18-b07. The application is only for Windows for now, but will also be adapted to Linux and Mac later.

Thanks much for any advice!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10271
    
    8

This is a new one. Never encountered like this before.
Did you try running the same code on different machines? It might be a problem with that machine. How about Linux? Mac?

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Bill Compton
Ranch Hand

Joined: Aug 26, 2000
Posts: 186
The code has been run a little on Mac and Linux (and worked fine) and has been run a LOT on two other computers, both Windows XP, with no issues either.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10271
    
    8

Looks like an issue related to this one particular machine then.
You might want to try out all standard Windoze trouble shooting tips. Reboot, reinstall etc.
If everything fails, there is always Ubuntu
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

I also think it's the PC. As far as I know, the JVM gets the value for user.home from the current user's registry settings. If that returns something that's incorrect then there are two options:
1) the wrong user's settings are being used; LocalService seems to indicate that the user account for services is being used instead of your own.
2) the user's settings are being read incorrectly.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bill Compton
Ranch Hand

Joined: Aug 26, 2000
Posts: 186
I appreciate the inputs. And yep, it does seem right that Windows was in a weird state those times. Rebooting definitely fixed it.

However, just fixing it on my computer isn't the real question. Finding a reliable way to get the path to a directory where the software can store and retrieve files is the question. If the user.home property returns something unreliable under Windows on my computer, it's likely that will happen to others, too, and that's not ok. I can't just blame Windows if a user who paid for our software suddenly loses all their files (or at least looks like it).

So, if user.home under Windows isn't reliable, what's a reliable way to find a "home" directory? Surely there must be a better way!

Idea: is the APPDATA environment variable reliable?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10271
    
    8

Though I see your point in your application being precise, because that is what the customer expects, I would not totally agree with you. Like you yourself mentioned before, you ran it flawlessly lots of times on different machines. Since you have rebooted and managed to fix the issue, I would recommend trying it more times to see if the problem resurfaces.

Update: Found http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4787931 That might provide you with some ideas. This is from 2002. One might have thought it was fixed in later versions
Bill Compton
Ranch Hand

Joined: Aug 26, 2000
Posts: 186
Thanks for the pointer to bug 4787931. This does seem to probably be the root cause. Given that the bug has been open and griped about since 2002 I don't think I should wait for a fix.

I agree that I need to continue monitoring for recurrences on my computer, and I'm certainly doing that.

Given that this software is a desktop application meant for commercial sale and use, any identified source of instability is, in my opinion, unacceptable. My Windows XP computer isn't unusual in any particular way, so if the problem occurs for me I have to assume it will bite some our users too - not ok.

Surely other Java applications have needed a reliable way to find a directory for storing files. For example, Eclipse has a default location for the workspace dir. How does the app find the path to such directories reliably?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10271
    
    8

One workaround.
1) The first time the application comes up, retrieve the user.home and display a message to the user saying something like "This application will store the local data in ${user.home} location. Is it OK? If not, please specify a different location.
2) Use the user specified location to maintain your local data
3) Also record the user choice under the same location where your app executable is installed maybe as an ini or preference file. *
4) Next time the app comes up, load the user choice from the preferences, go to that location and interact with your local data.

* You might want to check out the java preference API
Bill Compton
Ranch Hand

Joined: Aug 26, 2000
Posts: 186
Yes I think that workaround could definitely help understand what's gone wrong if this problem happens to a user. I'll think about incorporating the idea. Thanks!

Of course, it would be even better to find a reliable solution for getting the path to a good directory for storing the user's files.

Does anyone know how Eclipse (or a similar tool) decides on the full path to the default workspace (or other user-data) directory?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Eclipse works very much like Maneesh described. When you first run Eclipse it defaults to @user.home/workspace, but asks you if that is where you want the workspace to go. After that it stores the recent workspaces selected into a settings file (under <eclipse>/configuration/.settings/). For me, each time I start Eclipse it asks for the workspace, providing the list of workspaces I have recently used, and allowing me to browse to a new one. I think that is a preference you can change though so it just uses the last-used workspace automatically.


Steve
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

Bill Compton wrote:Idea: is the APPDATA environment variable reliable?

Check out System.getenv(String).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System.getProperty( "user.home" ) is unreliable?