Search code examples
javaeclipseeclipse-plugineclipse-rcpjvm-arguments

How Eclipse Plugin can programmatically pass Java VM Arguments


I'm programming an Eclipse Plugin which is actually a profiler that instruments the code using a Java Agent jar file.

I have programmed a custom Launch Configuration and also defined the Launch Configuration Tab Group for that. They are working fine and running local Java Projects. I want my plugin to automatically pass Java Agent jar file in vm arguments of JavaArgumentsTab so that code can be instrumented.

My Launch Configuration Code

public class MyJavaDelegate extends JavaLaunchDelegate
{
    @Override
    public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor)
            throws CoreException
    {
        super.launch(configuration, mode, launch, monitor);
        System.out.println("Custom Lanucher Launched");
    }
}

My Launch Configuration Tab Group Code

public class MyJavaTabGroup extends AbstractLaunchConfigurationTabGroup
{

    JavaMainTab jmTab;
    JavaArgumentsTab jaTab;
    JavaJRETab jjTab;
    CommonTab cTab;

    @Override
    public void createTabs(ILaunchConfigurationDialog dialog, String mode)
    {
        jmTab = new JavaMainTab();
        jaTab = new JavaArgumentsTab();
        jjTab = new JavaJRETab();
        cTab = new CommonTab();

        setTabs(new ILaunchConfigurationTab[] { jmTab , jaTab, jjTab, cTab });      
    }

}

As JavaArgumentsTab takes the vm arguments, I'm trying to set vm arguments by code but I don't find any method of JavaArgumentsTab class that allow me.


Solution

  • One way to do it is create your own subclass of JavaArgumentsTab and override setDefaults.

    setDefaults javadoc is:

    Initializes the given launch configuration with default values for this tab. This method is called when a new launch configuration is created such that the configuration can be initialized with meaningful values. This method may be called before this tab's control is created.

    For e.g.:

    import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
    import org.eclipse.jdt.debug.ui.launchConfigurations.JavaArgumentsTab;
    import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
    
    public class CustomJavaArgumentsTab extends JavaArgumentsTab {
    
        @Override
        public void setDefaults(ILaunchConfigurationWorkingCopy config) {
            // start with the normal defaults for this tab...
            super.setDefaults(config);
            // ... then set/override them with what I want
            // ATTR_VM_ARGUMENTS is defaulted to null, provide my desired default
            config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "my desired default");
        }
    }
    

    As you can see from this screenshot, "my desired default" appears in my launch configuration by default:

    vmargs