如何确保open命令调用Mac上的Java应用程序中的main函数 [英] How to ensure that open command invokes main function in the Java application on Mac

查看:471
本文介绍了如何确保open命令调用Mac上的Java应用程序中的main函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac上有一个在Eclipse中构建的java应用程序,并通过使用名为osxappbundle-maven-plugin的maven插件捆绑。
解压缩应用程序的dmg文件后,获取一个App文件,并将其复制到我的硬盘驱动器,我通过使用以下命令通过终端调用此应用程序:
open -a/ Full / Path / To / App / Match Player.app--args/ Full / Path / To / File / TEST 1.mplx

I have a java application on Mac that was built in Eclipse, and bundled by using a maven plugin named osxappbundle-maven-plugin. After I unpack the dmg file of the application, obtain an App file, and copy it to my hard drive, I invoke this App through a terminal by using the following command: open -a "/Full/Path/To/App/Match Player.app" --args "/Full/Path/To/File/TEST 1.mplx"



但是,在执行以下命令后:
open -a/ Full / Path / To / App / Match Player.app--args/ Full / Path / To /文件/ TEST 2.mplx
我的应用程序不使用新的测试文件打开应用程序,而只是关注以前打开的应用程序的窗口。

我相信主函数不再被调用,因为我写了主函数,使得在每次调用时,它重新初始化窗口。 (因此应用程序应重新初始化)。

This opens the application correctly.
However, after I execute the following command: open -a "/Full/Path/To/App/Match Player.app" --args "/Full/Path/To/File/TEST 2.mplx" my application does not open the application with the new test file, but just focuses the window of the previously opened application.
I am quite sure that the main function is not invoked again, since I wrote the main function such that on every invocation, it re-initializes the windows. (thus the application should re-initialize fully).

使用open -n选项打开多个应用程序不是一个选项。

Opening multiple applications by using "open -n" option is not an option.

推荐答案


我相信主函数不会再次调用

I am quite sure that the main function is not invoked again

是的,当已经有应用程序运行的实例时,open命令只是向正在运行的实例发送一个打开文件事件。您需要注册 OpenFilesHandler 接收此事件,您可以在处理程序内再次调用 main

Yes, when there's already an instance of the application running, the "open" command simply sends an "open files" event to the running instance. You need to register an OpenFilesHandler to receive this event, and you could call main again from inside the handler.

import com.apple.eawt.*;

public class MyMainClass {
  private static boolean listenerRegistered = false;

  public static void main(String[] args) throws Exception {
    if(!listenerRegistered) {
      Application.getApplication().setOpenFileHandler(new OpenFilesHandler() {
        public void openFiles(AppEvent.OpenFilesEvent evt) {
          List<String> filenames = new ArrayList<String>();
          for(File f : evt.getFiles()) {
            filenames.add(f.getAbsolutePath());
          }
          MyMainClass.main(filenames.toArray(new String[filenames.size()]));
        }
      });
      listenerRegistered = true;
    }

    // rest of main goes here
  }
}

现在 open -a/ Full / Path / To / App / Match Player.app/ Full / Path / To / File / TEST 1.mplx / code>(不带 - args )应该做正确的事。

Now open -a "/Full/Path/To/App/Match Player.app" "/Full/Path/To/File/TEST 1.mplx" (without the --args) should do the right thing.

这篇关于如何确保open命令调用Mac上的Java应用程序中的main函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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