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

时间:2011-10-18 17:35:52

标签: eclipse-rcp

我创建了一个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");

1 个答案:

答案 0 :(得分:5)

只需

Platform.getCommandLineArgs();

有关示例,请参阅http://www.vogella.de/blog/2008/06/21/passing-parameters-to-eclipse-rcp-via-the-command-line/