Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement reliable getLibraryFolder() and getDocumentsFolder() methods in MacPlatform #9

Open
benfry opened this issue Oct 8, 2019 · 3 comments

Comments

@benfry
Copy link
Member

@benfry benfry commented Oct 8, 2019

With the removal of FileManager in JDK 9+ we no longer have a means of getting the Documents and Library folders on macOS.

For now, it's possible to use System.getProperty("user.home") and tack Library or Documents onto the end of that, however I'm guessing that it won't be possible in future OS releases, or perhaps even with a signed version of the app, due to security-related changes to how file system access works.

(Appending these folder names works now even on localized systems, since the native language shown is not what's used internally by the OS.)

@sampottinger
Copy link
Collaborator

@sampottinger sampottinger commented Oct 28, 2019

There is an open issue at https://bugs.openjdk.java.net/browse/JDK-8187981 but it doesn't seem to be getting much traction. There is an alternative to System.getProperty but it's through swing:

import java.io.FileNotFoundException;
import javax.swing.filechooser.FileSystemView;


void setup() {
  
  // Use file view to get directories
  FileSystemView view = FileSystemView.getFileSystemView();
  File homeDirectory = view.getHomeDirectory();
  
  // Try writing to home directory
  File testHomeFile = view.createFileObject(homeDirectory, "test_home.txt");
  try {
    PrintWriter writer = new PrintWriter(testHomeFile);
    writer.print("test home");
    writer.close();
  } catch (FileNotFoundException e) {
    println("File not found exception on documents.");
  }
  
  // Try writing to documents directory
  File documentsDirectory = view.getChild(homeDirectory, "Documents");
  File testDocumentsFile = view.createFileObject(documentsDirectory, "test_docs.txt");
  try {
    PrintWriter writer = new PrintWriter(testDocumentsFile);
    writer.print("test documents");
    writer.close();
  } catch (FileNotFoundException e) {
    println("File not found exception on documents.");
  }
  
}

See https://gist.github.com/sampottinger/49aef03638b150257cca919954319f01. It does work on 10.14 with a signed application.

Open to suggestions but, unfortunately, JDK 11 isn't providing an alternative as far as I can tell? @benfry - would you like me to switch out for FileSystemView or leave this ticket as is and address in a future JDK?

@benfry
Copy link
Member Author

@benfry benfry commented Nov 15, 2019

I think FileSystemView would be a step backwards, so we should leave it open until we have a better fix. (Want to get AWT/Swing out of there wherever possible.)

Might be something we just need to add via JNA, since it should be a single OS call.

@sampottinger
Copy link
Collaborator

@sampottinger sampottinger commented Nov 16, 2019

I agree. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.