2011-10-18 38 views
1

我已经创建了一个eclipse应用程序和一个产品配置。在RCP应用程序中读取程序参数?

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

是否可以从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(); 
     } 
     } 
    }); 
    } 

是否有更好的方法来提取应用ARGS比:

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

回答