u123
u123

Reputation: 16329

Reading program arguments in a RCP application?

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");

Upvotes: 1

Views: 2711

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170815

Just

Platform.getCommandLineArgs();

See http://www.vogella.de/blog/2008/06/21/passing-parameters-to-eclipse-rcp-via-the-command-line/ for an example.

Upvotes: 5

Related Questions