使用 Java 1.5 跨平台打开文件的方法
我使用的是 Java 1.5,我想启动关联的应用程序来打开该文件。 我知道 Java 1.6 引入了 Desktop API,但我需要一个针对Java 1.5的解决方案。
到目前为止,我找到了一种在 Windows 中执行此操作的方法:
Runtime.getRuntime().exec(new String[]{ "rundll32",
"url.dll,FileProtocolHandler", fileName });
Is there a cross-platform way to do it? 或者至少为 Linux 提供类似的解决方案?
I'm using Java 1.5 and I'd like to launch the associated application to open the file. I know that Java 1.6 introduced the Desktop API, but I need a solution for Java 1.5.
So far I found a way to do it in Windows:
Runtime.getRuntime().exec(new String[]{ "rundll32",
"url.dll,FileProtocolHandler", fileName });
Is there a cross-platform way to do it? Or at least a similar solution for Linux?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
和
and
JDIC 是一个在 Java 1.5 中提供类似桌面功能的库。
JDIC is a library that provides Desktop-like functionality in Java 1.5.
+1 这个答案
另外,我建议使用多态性进行以下实现:
这样您可以通过减少类之间的耦合。
客户端代码:
桌面实现:
这只是一个示例,在现实生活中不值得创建一个新类只是为了参数化一个值(命令字符串 %s )但是让我们想象一下每个方法都以特定于平台的方式执行其他步骤。
采取这种方法,可能会删除不需要的 if/elseif/else 结构,随着时间的推移,这些结构可能会引入错误(如果代码中有 6 个这样的结构并且需要更改,您可能会忘记更新其中一个,或者通过复制/粘贴时你可能会忘记更改要执行的命令)
+1 for this answer
Additionally I would suggest the following implementation using polymorphism:
This way you can add new platform easier by reducing coupling among classes.
The Client code:
The Desktop impl:
This is only an example, in real life is not worth to create a new class only to parametrize a value ( the command string %s ) But let's do imagine that each method performs another steps in platform specific way.
Doing this kind of approach, may remove unneeded if/elseif/else constructs that with time may introduce bugs ( if there are 6 of these in the code and a change is neede, you may forget to update one of them, or by copy/pasting you may forget to change the command to execute)
作为补充:不要使用
gnome-open
,而是使用xdg-open
。 它是 XdgUtils 的一部分,而 XdgUtils 又是 LSB 桌面支持包的一部分(从 3.2 开始) )。您仍然可以(应该)使用
gnome-open
作为后备,但xdg-open
也可以在非 GNOME 桌面上运行。Just as an addition: Rather than
gnome-open
, usexdg-open
. It's part of the XdgUtils, which are in turn part of the LSB Desktop support package (starting with 3.2).You can (should) still use
gnome-open
as a fallback, butxdg-open
will also work on non-GNOME desktops.SWT 使您可以通过以下方式查找标准程序打开文件:
严格来说这不是'跨平台,因为 SWT 依赖于平台,但对于每个平台,您可以使用不同的 SWT jar。
SWT gives you the possibility to lokk for the standard program to open a file via:
Strictly this isn't Cross-Platform since SWT is platform dependent, but for every platform you can use a diffenrent SWT jar.
您可以使用操作系统默认的方式为您打开它。
You can use the OS default way to open it for you.
另一个答案(由 boutta)建议使用 SWT。 我不建议仅出于此目的引用该库,但如果您已经在使用它,只需执行:
请注意,此方法仅在
Display 时才有效(并返回
对象已创建(例如通过创建true
)Shell
)。 另请注意,它必须在主线程中运行; 例如:在上面的示例中,我启动了一个 URL,但启动文件的方式相同。
Another answer (by boutta) suggests using SWT. I wouldn't recommend referencing the library for this purpose only, but if you are using it already, simply execute:
Take note that this method will only work (and return
true
) if aDisplay
object has already been created (for instance by creating aShell
). Also take note that it must run in the main thread; e.g.:In the example above I've launched a URL, but launching files works in the same way.
我们确实将该命令放在配置文件中的某个位置之外。
您的“JAR 和源代码”将是“跨平台的”,但您的部署却不是。
您还可以执行类似此答案。 您可以将“Deskop”实现的工厂类的类名放入安装文件中。 (如果你愿意的话,可以是指南或弹簧)
We do put the command outside somewhere in the configuration file.
Your "JAR and source code" will be "cross-platform", but your deployment doesn't.
You can also do something like this answer. You can put the classname of the factory class of the "Deskop" implementation into the setup file. (may be guide or spring, if you like)