On a MacBook Pro (2015) with fresh-installed Big Sur and AdoptOpenJDK 11 I developed a Java program for educational purpose that uses the JFileChooser. I did not use any IDE-specific code. As the rest of the program does not matter, here is a minimum-example that produces the same problem for me (Note: Here only as an example, clicking the button will open the File Chooser, choosing a file and clicking OK will change the button's text to 'OK'):
import javax.swing.*;
import java.awt.event.*;
public class Test extends JFrame implements ActionListener {
private JFileChooser jf;
private JButton jb;
public Test() {
setSize(480,320);
jf = new JFileChooser();
jf.setDialogType(JFileChooser.OPEN_DIALOG);
jb = new JButton("CLICK ME");
jb.addActionListener(this);
add(jb);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(jb)) {
jf.setVisible(true);
final int result = jf.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
jb.setText("OK");
}
}
}
public static void main(String[] args) {
new Test();
}
}
If I start the program via Terminal (java Test
or compiled as a jar with java -jar Test.jar
) everything works fine. I can open the File Chooser and it shows up my files and folders on my disk.
If I start the compiled jar via double-click, the program launches as well, but if I open the File Chooser I cannot see any files on my disk and hence I cannot load and save data to disk.
As I only have these problems on my Mac (not on Windows 10 or Lubuntu Linux) this might be a very specific problem due to false Java settings on my Mac. However, as I installed a fresh copy of Big Sur and AdoptOpenJDK 11 for Mac without changing anything, I wonder if this problem may occur for other people who want to run my program (teachers and students).
So what might be the problem and how to solve this (for me and potentially others)?
I already figured out with the activity monitor that the double-clicked jar is loaded with the JavaLauncher (but I cannot find it on disk and I cannot change any system settings for that).
I also searched for similar problems here. But these were mostly associated with saving files on wrong paths.
Would be nice to find a solution. Thank you for answering!