双击文件后如何使用我的Java程序打开文件? [英] How can my Java program be used to open a file when the file is double clicked?

查看:61
本文介绍了双击文件后如何使用我的Java程序打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当双击MS Word文档(.doc文件)或Photoshop文件(.psd)等的图标时,操作系统会调用合适的程序来读取文件.

When you double click the icon of an MS Word document (.doc files), or a Photoshop file (.psd), etc., the suitable program to read the file is called by the OS.

我正在使用一个解释器来读取二进制指令并执行它们.我可以轻松地添加从磁盘将文件导入程序的选项,但是当操作系统调用该程序时,如何使程序能够打开文件(如上述示例)?

I'm working on an interpreter that reads binary instructions and executes them. I can easily add the option to import files from disk to the program, but how can I make the program be able to open a file when called by the OS (like in the examples above)?

推荐答案

您需要在调用诸如 java.exe -jar yourjar.jar%1之类的程序时,注册文件类型("open"动词).".然后启动Main java类,并且main方法获取文件名作为开始参数数组.

You need to register the file type (the "open" verb) as calling a program like java.exe -jar yourjar.jar "%1". And then the Main java class is started, and the main method gets the filename(s) as a start arguments array.

然后可以显示或编辑它,但是可以在已经运行的应用程序中打开它.您不能*轻松地使用DDE机制将新文件名传递到打开的Java窗口中(但是您可以使用两个Java实例之间的替代进程间机制对该代码进行编码).

You can then display or edit it, however you might open it in an already running application instead. You cannot easily* use the DDE mechanism for passing a new filename to an open Java window (but you can code that with al alternative inter process mechanism between the two Java instances).

在注册表中,它将类似于 HKLM \ SOFTWARE \ Classes \ yourext \ shell \ open \ command = java.exe -jar C:\ yourjar.jar%1"

In the registry it would be something like HKLM\SOFTWARE\Classes\yourext\shell\open\command=java.exe -jar C:\yourjar.jar "%1"

MSDN具有有关一般机制的一些很好的信息.在 http上查找注册动词://msdn.microsoft.com/zh-cn/library/windows/desktop/dd758090%28v=vs.85%29.aspx (当然,假设您正在谈论Windows).

The MSDN has some good information on the general mechanism. Look for registering verbs on http://msdn.microsoft.com/en-us/library/windows/desktop/dd758090%28v=vs.85%29.aspx (asuming you are talking about Windows, of course).

)* Java Web Start实际上允许定义 SingleInstanceService

)* Java Web Start actually allows to define file handlers and to run with a SingleInstanceService

顺便说一句:在这个答案中,有人建议可以通过install4j来完成:

BTW: in this answer somebody suggests it can be done with install4j: listen for "open file with my java application" event on windows

这篇关于双击文件后如何使用我的Java程序打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆