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

后端 未结 4 2123
星月不相逢
星月不相逢 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();
                }
            });
    
        }
    

提交回复
热议问题