如何打开给定文件的用户系统首选编辑器?

发布于 2024-07-14 02:43:33 字数 817 浏览 5 评论 0原文

我试图弄清楚如何打开给定文件的系统首选编辑器。

假设我们有一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

魂归处 2024-07-21 02:43:33

似乎如果您不能使用 java.awt.Desktop 您必须区分操作系统:
Windows:

RUNDLL32.EXE SHELL32.DLL,OpenAs_RunDLL <file.ext>

Linux:

edit <file.ext>

Mac:

open <file.ext>

HTH。 显然,这不太便携......

Seems that if you can't use java.awt.Desktop you have to distinguish between the OSes:
Windows:

RUNDLL32.EXE SHELL32.DLL,OpenAs_RunDLL <file.ext>

Linux:

edit <file.ext>

Mac:

open <file.ext>

HTH. Obviously, that is not very portable...

没︽人懂的悲伤 2024-07-21 02:43:33

查看 java.awt.Desktop 对象。 在您的情况下,您想要调用 edit()

如果你想确保给定的平台支持这个调用,那么你可以执行如下操作(我没有测试过这段代码):

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;
}

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):

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;
}
╰ゝ天使的微笑 2024-07-21 02:43:33

这不是跨平台的,但在 Mac OS X 上您可以执行

Runtime.getRuntime().exec("open filename");

open(1) 可执行文件使用 LaunchServices 选择要执行的正确程序,然后使用它打开名为 filename 的文件。

This isn't cross-platform, but on Mac OS X you can do

Runtime.getRuntime().exec("open filename");

The open(1) executable uses LaunchServices to pick the right program to execute, and then uses that to open the file named filename.

掩耳倾听 2024-07-21 02:43:33

对于JavaFX应用程序,我们可以使用HostServices。 这个问题 介绍了如何使用 HostServices。 这应该适用于 Ubuntu(已测试)/Windows(未测试)和 Mac(未测试)。

import java.io.File;
import java.io.IOException;
public class App extends Application {
}

File file = new File("/your/file/path");
HostServices hostServices = getHostServices();
hostServices.showDocument(file.getAbsolutePath());

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).

import java.io.File;
import java.io.IOException;
public class App extends Application {
}

File file = new File("/your/file/path");
HostServices hostServices = getHostServices();
hostServices.showDocument(file.getAbsolutePath());

getHostServices() is a method of JavaFX Application class.

甜味超标? 2024-07-21 02:43:33

这将在 Windows 中工作

Runtime.getRuntime().exec( "CMD /C START filename.ext " );

This will work in windows

Runtime.getRuntime().exec( "CMD /C START filename.ext " );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文