如何打开给定文件的用户系统首选编辑器?
我试图弄清楚如何打开给定文件的系统首选编辑器。
假设我们有一个用 Java 编写的文件管理器。 用户转到文件夹并查看文件列表。 例如,有一个文件 Icon.jpg
。 用户双击文件名,文件将在系统的首选编辑器(即 Gimp)中打开。 主要问题是 - 如何做到这一点?
我们可以执行Runtime.getRuntime().exec("something file")
,但是这样您应该知道在用户环境中首选哪个程序。 但如何呢?
我们还可以执行Desktop.getDesktop().edit(File file)
,但这样我们就无法跟踪进程,也无法知道该子进程是否已关闭。 其他问题 - 函数在 Linux 上不起作用(至少在 Ubuntu 8.10 上)。 还有 Desktop.getDesktop().open(File file)
,但它强制打开文件查看器,而不是该文件类型的系统查看器。
我整个星期都在寻找解决方案,但没有找到任何合适且通用的解决方案。 你知道这个问题的其他方法吗? 对于我的项目来说,如果它能在 Windows+Linux+Mac 上运行就足够了。
感谢您的回答和建议。
编辑于2009-02-08 23:04
其他建议:我可以在Windows和Linux中强制“应用程序选择”窗口,就像在Mac中使用“打开文件”一样吗? 例如,然后您尝试打开文件,系统会要求您从系统首选应用程序列表中选择应用程序? (类似于 Windows 资源管理器中的“打开方式...”)。 你知道吗?
I'm trying to figure out how to open the system preferred editor for a given file.
Say, we have a file manager, written in Java. User goes to folder and sees the list of files. And, for example, there is a file Icon.jpg
. User double clicks on the filename and file opens in system's preferred editor (i.e. Gimp). The main issue is - how to do that?
We can do Runtime.getRuntime().exec("something file")
, but this way you should know which program is preferred in user environment. But how?
We also are able to do Desktop.getDesktop().edit(File file)
, but this way we cannot track process and aren't able to know then this child process is closed. Other issue - function doesn't work on linux (at least on Ubuntu 8.10). There is also Desktop.getDesktop().open(File file)
, but it forces to open file viewer, instead of system viewer for that file type.
I am searching for a solution all week, but didn't got any suitable and generic one. Do you know the other approaches to this question? For my project it would be enough if it would work on Windows+Linux+Mac.
Thank you for your answers and advices.
Edit on 2009-02-08 23:04
Other suggestion: can I force "application selection" window in Windows and in Linux, as in Mac with "open file"? For example, then you trying to open file, you are being asked to choose application from list of system preferred ones? (something like "Open with..." in Windows explorer). Do you know?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
似乎如果您不能使用
java.awt.Desktop
您必须区分操作系统:Windows:
Linux:
Mac:
HTH。 显然,这不太便携......
Seems that if you can't use
java.awt.Desktop
you have to distinguish between the OSes:Windows:
Linux:
Mac:
HTH. Obviously, that is not very portable...
查看 java.awt.Desktop 对象。 在您的情况下,您想要调用
edit()
如果你想确保给定的平台支持这个调用,那么你可以执行如下操作(我没有测试过这段代码):
Check out the java.awt.Desktop object. In your case, you want to invoke
edit()
If you want to ensure that a given platform supports this call, then you can do something like the following (I have not tested this code):
这不是跨平台的,但在 Mac OS X 上您可以执行
open(1)
可执行文件使用 LaunchServices 选择要执行的正确程序,然后使用它打开名为filename
的文件。This isn't cross-platform, but on Mac OS X you can do
The
open(1)
executable uses LaunchServices to pick the right program to execute, and then uses that to open the file namedfilename
.对于JavaFX应用程序,我们可以使用HostServices。 这个问题 介绍了如何使用 HostServices。 这应该适用于 Ubuntu(已测试)/Windows(未测试)和 Mac(未测试)。
getHostServices() 是 JavaFX Application 类的方法。
For JavaFX applications, we can use HostServices. This question covers how to use HostServices. This should work on Ubuntu (tested)/Windows (not tested) and Mac (not tested).
getHostServices() is a method of JavaFX Application class.
这将在 Windows 中工作
This will work in windows