13

For whatever reason, I sometimes need to find the current user's My Documents folder on Windows in a Java program to read some files. But as far as I can tell, there is no way to do it that isn't severely flawed.

The first wrong way: System.getProperty("user.home");
Why it won't work:

  • It only returns the \username\ folder; I'd need to add "\Documents\" on to the end to get the Documents folder... and that only works in English.
  • Sun bugs 6519127 and 4787931. Java finds the user home folder on Windows by reading a deprecated registry key* to find the Desktop then taking the parent; this method has multiple known problems that will easily cause a completely wrong folder to be returned. The bugs are 3.75 years and 8 years old with no fix.

The second wrong way: Using a registry-reading program to get the Personal folder of the user, which is My Documents (but i18n'd).
Why it won't work:
While it fixes the English-only problem, it's still using the same deprecated registry area, so the bugs still apply to it.

The deprecated registry key says to use a native call (SHGetKnownFolderPath) which I obviously can't do from Java.

The third wrong way:

JFileChooser fr = new JFileChooser();  
FileSystemView fw = fr.getFileSystemView();  
File documents = fw.getDefaultDirectory();

Why it won't work: It works great!
Except when it doesn't. While I had a program that used this open and running in the background, I opened a DirectX game (Fallout: New Vegas). The Java program immediately terminated with no stack trace. Always reproducible (for me on that game, and who knows what else). Couldn't find a Sun bug#.

So is there any method to find a user's Documents folder, on Windows, from Java, that doesn't have known problems?

(This is a nice big question.)

*(The key is "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")

Aleksei Vasiliev
  • 221
  • 1
  • 3
  • 6
  • Same as [how to find “My Documents” folder ](http://stackoverflow.com/questions/1503555/how-to-find-my-documents-folder). It mentions winfoldersjava, a JNI wrapper around SHGetSpecialFolderPath. Since you're using Windows-specific functionality, JNI should be okay. – Matthew Flaschen Nov 06 '10 at 18:40
  • 1
    Did you really isolate these crashes to the code snippet from the third way? Because this method seems to be used by Swings own Open file dialog (JFileCHooser) so it seems a lot of Java apps would suffer from it? – Stijn de Witt Mar 21 '11 at 14:59
  • http://stackoverflow.com/questions/1503555/how-to-find-my-documents-folder – Ray Hulha Aug 28 '12 at 13:35

4 Answers4

4

There's no pure java way to do it, but you could use the JNA wrapper over JNI to do it without having to write any native code yourself. There's a good example of how to get the Documents folder on Windows halfway down the responses at:

What is the best way to find the users home directory in Java?

Community
  • 1
  • 1
marcprux
  • 9,845
  • 3
  • 55
  • 72
3

A time consuming, but reliable way of finding the 'Documents' folder of a windows user: Make your java app execute a bat script that uses Reg.exe (a windows system file) to find the value of the reg key which has the path in it. Then use a pipeline in the same bat file to send that data to the 'findstr' function which windows command prompt has. Use another pipeline to output the returned value to a text file. Then, simply make your java app read that text file, and delete it once its done :) Worked well enough for me.

Code for the bat file:

@ echo off

Title Find Documents Folder

Reg Query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" |findstr "Personal">>DocPath.dat

exit
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Thishin
  • 31
  • 1
0

There is a custom Java API that someone built (their website no longer works), but there code remains on Google Code:

http://winfoldersjava.googlecode.com/files/WinFoldersJava_1.1.zip

There are two DLL's that need to be referenced, one for each architecture(x86 and x64).

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
-1

user.home is not "my documents", but users home folder, like on Unix ~/. To get to "My documents" you can use System.getProperty("user.home")+"\Documents"; irrespective of the language system. Try it.

Xazax
  • 25
  • 2
  • He has already stated that he is needs multilingual support - this won't work. – Evan Mulawski Nov 06 '10 at 18:49
  • This also won't work even without the English-only problem; as I pointed out in my question, Java uses a deprecated registry key to find user.home. It may fail on Roaming Profiles, on an XP->Vista upgrade, if the user has renamed their Desktop folder, etc. (Partial fixes may have been delivered but it is overall broken) – Aleksei Vasiliev Nov 06 '10 at 18:58
  • Please don't post answers that cause more problems than they solve. – Ian Kemp Aug 17 '11 at 17:17