Java/Swing: Desktop.open() causes GTK-ERROR

我是研究僧i 提交于 2020-01-03 16:47:03

问题


I am currently developing a Swing Desktop application. This application is also using a tray icon which is handled by SystemTray of dorkbox.

Now I need to open a file with the default application. To achieve this I am using the Desktop.open() method of AWT like this.

if (Desktop.isDesktopSupported()) {
    System.out.println("Get desktop.");
    Desktop desktop = Desktop.getDesktop();
    System.out.println("Got desktop.");
    desktop.open(file);
}

But now here comes the problem: On some devices (which apparently have GTK2 and GTK3 installed this few lines make application crash - the program crashes while executing the Desktop.isDesktopSupported() line with a gtk-error ** gtk+ 2.x symbols detected. using gtk+ 2.x and gtk+ 3 is not supported.

To be honest, I have no clue, what is going wrong here - but if only GTK3 is installed the application runs like a charm. The SystemTray seems to be using GTK3 as well because I did not explicitly set it up to use GTK2. So, what's causing this mix of GTK2 and 3? Is there a way to force the Desktop class to use the right version of GTK (the same, as being used by the rest of the application)?


回答1:


It won't resolve your current problem but there is another way to open file with the default application.

On Windows:

Runtime.getRuntime().exec("C:\SomeFolder\Somefile.txt");

On Mac and Linux:

Runtime.getRuntime().exec("xdg-open /folder/file.txt");

Hope it will help you, if you won't resolve your current problem.



来源:https://stackoverflow.com/questions/40554515/java-swing-desktop-open-causes-gtk-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!