读取 RCP 应用程序中的程序参数?

发布于 2024-12-10 20:59:12 字数 1589 浏览 4 评论 0原文

我创建了一个 Eclipse 应用程序和一个产品配置。

在“启动”选项卡上的产品配置中,可以指定“程序参数”和“VM 参数”。

是否可以从 Application 类访问这些参数?目前这个类看起来像这样:

public class Application implements IApplication {

  @Override
  public Object start(IApplicationContext context) throws Exception {
    Map<?, ?> arguments = context.getArguments(); // this is empty!

    // This actually does the trick but is a bit ugly - must be parsed
    String[] strings = (String[]) context.getArguments()
    .get("application.args");

    Display display = PlatformUI.createDisplay();
    try {
      ApplicationWorkbenchAdvisor advisor = new ApplicationWorkbenchAdvisor();
      int returnCode = PlatformUI.createAndRunWorkbench(display, advisor);
      if (returnCode == PlatformUI.RETURN_RESTART) {
        return IApplication.EXIT_RESTART;
      } else {
        return IApplication.EXIT_OK;
      }
    } finally {
      display.dispose();
    }

  }


  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.equinox.app.IApplication#stop()
   */
  @Override
  public void stop() {
    final IWorkbench workbench = PlatformUI.getWorkbench();
    if (workbench == null) {
      return;
    }
    final Display display = workbench.getDisplay();
    display.syncExec(new Runnable() {
      @Override
      public void run() {
        if (!display.isDisposed()) {
          workbench.close();
        }
      }
    });
  }

有没有比以下更好的方法来提取应用程序参数:

// This actually does the trick but is a bit ugly
String[] strings = (String[]) context.getArguments()
.get("application.args");

I have created an eclipse application and a product configuration.

In the product configuration on the "Launching" tab its possible to specify "Program arguments" and "VM Arguments".

Is it possible to access these arguments from the Application class? Currently this class looks like this:

public class Application implements IApplication {

  @Override
  public Object start(IApplicationContext context) throws Exception {
    Map<?, ?> arguments = context.getArguments(); // this is empty!

    // This actually does the trick but is a bit ugly - must be parsed
    String[] strings = (String[]) context.getArguments()
    .get("application.args");

    Display display = PlatformUI.createDisplay();
    try {
      ApplicationWorkbenchAdvisor advisor = new ApplicationWorkbenchAdvisor();
      int returnCode = PlatformUI.createAndRunWorkbench(display, advisor);
      if (returnCode == PlatformUI.RETURN_RESTART) {
        return IApplication.EXIT_RESTART;
      } else {
        return IApplication.EXIT_OK;
      }
    } finally {
      display.dispose();
    }

  }


  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.equinox.app.IApplication#stop()
   */
  @Override
  public void stop() {
    final IWorkbench workbench = PlatformUI.getWorkbench();
    if (workbench == null) {
      return;
    }
    final Display display = workbench.getDisplay();
    display.syncExec(new Runnable() {
      @Override
      public void run() {
        if (!display.isDisposed()) {
          workbench.close();
        }
      }
    });
  }

Are there a better way to extract the application args than:

// This actually does the trick but is a bit ugly
String[] strings = (String[]) context.getArguments()
.get("application.args");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

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