Open a path with Desktop.open() from java on ubuntu (linux)

后端 未结 4 2135
星月不相逢
星月不相逢 2021-02-15 10:09

From my application written in java I want to open a folder, using the operating system file explorer.

I use Desktop.open(new File(path))

This works fine on wind

相关标签:
4条回答
  • 2021-02-15 10:16

    I had the same problem. But in my case it was Ubuntu 18.04 and java 1.8.0_161-b12 In Windows 10, everything is working fine. But on Ubuntu

    Desktop.getDesktop().open(new file) 
    

    the program stopped responding. I decided to wrap the call in the executor:

    private ExecutorService executorService; 
       BasicThreadFactory factory = new BasicThreadFactory.Builder()
                .namingPattern("YourPatternIndeficator")
                .build();
        executorService = Executors.newSingleThreadExecutor(factory);
    if (Desktop.isDesktopSupported()) {
            File myFile = new File(path);
            executorService.execute(() -> {
                try {
                    Desktop.getDesktop().open(myFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
    
        }
    
    0 讨论(0)
  • 2021-02-15 10:28

    I can't confirm the error. I took your code and constructed a main method around it, and everything works as expected. I don't exactly know where the default applications are set (in my case PCMan was opened instead of usual Nautilus, but it should fulfil its purpose in the end).

    Over at java.awt.Desktop.open doesn’t work with PDF files? I have found a link pointing to an issue in Suns (Oracles) bug tracker stating that the method for opening files using AWT isn't reliable even on Windows. Maybe you should think of alternative ways of opening such applications. Furthermore AWT is deprecating soon almost for sure.

    If you are utilizing SWT in your application, you could use org.eclipse.swt.program.Program.

    0 讨论(0)
  • 2021-02-15 10:34

    I was running into what sounds like the same issue on Mint 13. From what I can tell, changes to mime handling for opening directories has broken the java Desktop api. I was able to work around the problem by editing

    ~/.local/share/applications/defaults.list
    

    and adding this line

    x-directory/normal=nautilus.desktop
    

    I'm running Mint 13 Cinnamon with java version "1.7.0_05"

    0 讨论(0)
  • 2021-02-15 10:35

    I was running into the same issue and decided to give Java 7 a whirl. I'm running java version "1.7.0_147-icedtea" on Ubuntu 11.10_x64 and am able to open file locations in Nautilus quite happily now.

    0 讨论(0)
提交回复
热议问题