stove
stove

Reputation: 576

Desktop.getDesktop().open(file) on Ubuntu not working

I have a Java application, and when I use java.awt.Desktop:

Desktop.getDesktop().open(file);

It works fine on Windows (opens a file in my default program), but on Ubuntu (with openJdk 13), the Java application gets stuck and I do not even get any log error or anything. I have to force quit the app in order to recover.

The file path it correct, otherwise I would actually get an Exception. Also, isDesktopSupported a isSupported(Action.OPEN) returns true.

What can I do? Can I check some system settings or logs? Or perhaps get some logs from java.awt.Desktop? Or does this not work on Ubuntu/Linux?

Are there any alternatives?

Upvotes: 1

Views: 1084

Answers (2)

mynor peralta
mynor peralta

Reputation: 11

I am just going to add an example function

    private static void OpenFile(String filePath){
            try
            {
                //constructor of file class having file as argument
                File file = new File(filePath);
                if(!Desktop.isDesktopSupported())//check if Desktop is supported by Platform or not
                {
                    System.out.println("not supported");
                    return;
                }
                Desktop desktop = Desktop.getDesktop();
                if(file.exists()) {         //checks file exists or not
                    EventQueue.invokeLater(() -> {
                        try {
                            desktop.open(file);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    });
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
    }

Upvotes: 1

stove
stove

Reputation: 576

From here:

In order to use the API, you have to call java.awt.EventQueue.invokeLater() and call methods of the Desktop class from a runnable passed to the invokeLater():

void fxEventHandler() {
   EQ.invokeLater(() -> {
      Desktop.open(...);
   });
}

Upvotes: 2

Related Questions