Java 如何打开给定文件的用户系统首选编辑器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/526037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open user system preferred editor for given file?
提问by Arturas Smorgun
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?
比如说,我们有一个用 Java 编写的文件管理器。用户转到文件夹并查看文件列表。并且,例如,有一个文件Icon.jpg
. 用户双击文件名并在系统首选编辑器(即 Gimp)中打开文件。主要问题是 - 如何做到这一点?
We can do Runtime.getRuntime().exec("something file")
, but this way you should know which program is preferred in user environment. But how?
我们可以做到Runtime.getRuntime().exec("something file")
,但是这样您就应该知道在用户环境中哪个程序是首选的。但是如何?
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.
我们也可以这样做Desktop.getDesktop().edit(File file)
,但是这样我们无法跟踪进程,也无法知道此子进程是否已关闭。其他问题 - 函数在 linux 上不起作用(至少在 Ubuntu 8.10 上)。还有Desktop.getDesktop().open(File file)
,但它强制打开文件查看器,而不是该文件类型的系统查看器。
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.
我整个星期都在寻找解决方案,但没有找到任何合适的通用解决方案。你知道这个问题的其他方法吗?对于我的项目,如果它可以在 Windows+Linux+Mac 上运行就足够了。
Thank you for your answers and advices.
感谢您的回答和建议。
Edit on 2009-02-08 23:04
编辑于 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?
其他建议:我可以在 Windows 和 Linux 中强制使用“应用程序选择”窗口吗,就像在 Mac 中使用“打开文件”一样?例如,当您尝试打开文件时,系统会要求您从系统首选应用程序列表中选择应用程序?(类似于 Windows 资源管理器中的“打开方式...”)。你知道吗?
采纳答案by Johannes Weiss
Seems that if you can't use java.awt.Desktop
you have to distinguish between the OSes:
Windows:
似乎如果你不能使用java.awt.Desktop
你必须区分操作系统:Windows:
RUNDLL32.EXE SHELL32.DLL,OpenAs_RunDLL <file.ext>
Linux:
Linux:
edit <file.ext>
Mac:
苹果电脑:
open <file.ext>
HTH. Obviously, that is not very portable...
哈。显然,这不是很便携......
回答by Adam Rosenfield
This isn't cross-platform, but on Mac OS X you can do
这不是跨平台的,但在 Mac OS X 上你可以做到
Runtime.getRuntime().exec("open filename");
The open(1)
executable uses LaunchServicesto pick the right program to execute, and then uses that to open the file named filename
.
该open(1)
可执行文件使用LaunchServices选择合适的程序来执行,然后再使用它来打开指定的文件filename
。
回答by Eddie
Check out the java.awt.Desktopobject. In your case, you want to invoke edit()
查看java.awt.Desktop对象。在你的情况下,你想调用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):
如果要确保给定平台支持此调用,则可以执行以下操作(我尚未测试此代码):
public boolean editFile(final File file) {
if (!Desktop.isDesktopSupported()) {
return false;
}
Desktop desktop = Desktop.getDesktop();
if (!desktop.isSupported(Desktop.Action.EDIT)) {
return false;
}
try {
desktop.edit(file);
} catch (IOException e) {
// Log an error
return false;
}
return true;
}
回答by Shami
This will work in windows
这将在 Windows 中工作
Runtime.getRuntime().exec( "CMD /C START filename.ext " );