侦听“使用我的Java应用程序打开文件" Windows事件 [英] listen for "open file with my java application" event on windows

查看:144
本文介绍了侦听“使用我的Java应用程序打开文件" Windows事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题令人困惑,但不知道如何用几句话来解释:

Title is confusing, but don't know how to explain this in a few words:

我有一个读取* .example文件的Java应用程序.我还添加了文件关联,这要感谢install4j,因此当用户双击任何扩展名为* .example

I have a java application that reads *.example files. I've also added a file association thanks to install4j so my application is launched when the user double clicks any file with extension *.example

似乎install4j在args[]中发送文件路径,因此打开该文件并将其显示在我的应用程序中应该很容易. 如果该应用已在运行,会发生什么?我只能允许该应用程序的一个实例,因此,如何知道用户正在打开文件?

It seems that install4j sends the file path in the args[] so it should be easy to open that file and show it in my app. BUT what happens if the app is already running? I can only allow one instance of the application so, how can I know that the user is opening a file?

我发现了这一点: http://resources .ej-technologies.com/install4j/help/api/com/install4j/api/launcher/StartupNotification.html

但是我仍然不知道我应该如何使用它以及应该在我的应用程序中添加些什么来监听此事件.在哪里可以找到示例?

But I still don't understand how should I use it and what should I add in my app to listen for this event. Where can I find an example?

推荐答案

根据您链接到的文档,看来您可以执行以下操作:

Based on the documentation you linked to, it looks like you can do this:

StartupNotification.registerStartupListener(new StartupNotification.Listener() {
    public void startupPerformed(String parameters) {
        System.out.println("Startup performed with parameters " + parameters);
    }
});

由于将从不同的线程调用startupPerformed,因此您需要确保处理这些通知的代码是线程安全的.

Since startupPerformed will be called from different threads, you will need to make sure that the code that handles these notifications is thread-safe.

文档还显示:

对于多个文件,文件用双引号引起来并用空格分隔.

For multiple files, files are surrounded by double-quotes and separated by spaces.

因此,您也需要自己解析参数字符串.

So you will need to parse the parameter string yourself as well.

这篇关于侦听“使用我的Java应用程序打开文件" Windows事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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