How to launch the default (native) application for a given file from Java?

本秂侑毒 提交于 2019-11-27 14:13:13
Joseph Gordon

What you want is java.awt.Desktop:

Desktop.getDesktop().open( file );

I have found an API in Eclipse's SWT now that seems to do the trick: org.eclipse.swt.program.Program "provides access to facilities for discovering operating system specific aspects of external program launching."

It has methods to find the program for a given file extension, get the program's icon, and even launch the program.

Sounds like you're after the Java Activation Framework ("JAF"). This API lets you determine what files are and what actions you can do on them. Or alternatively the Java Desktop Integration Component ("JDIC"). JDIC allows you to create and no doubt query file associations.

Both projects seem to be in a semi-abandoned state howeer (sigh). But that's par for the course for Sun these days. Only other thing I know of is some Windows specific third party library that's based on JNI called Winpack. It does a bunch of other things too.

You can get the associated icon using the FileSystemView class (Java 1.4+).

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