How to run current configuration changing main class?

I want to launch the current configuration after changing its main class and adding something to the class path.
How do I accomplish this?

6 comments

strange question

open configuration window
do what you want: "changing its main class and adding something to the class path."


what's the problem?

0

Wi Wi: Using the API....
I manage to do it but I had to decompile ApplicationConfiguration (which is not even in the API jar) to figure out how to do it.

0

Hello Jose,

Wi Wi: Using the API....
I manage to do it but I had to decompile ApplicationConfiguration
(which is not even in the API jar) to figure out how to do it.


Why do you need to decompile anything when there's source code available?
http://www.jetbrains.org/

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

ah, sorry, i didn't look at forum name :)

why decompile?
idea's sources are freely downloadable from official site

0

barrerajose schrieb:

Wi Wi: Using the API....
I manage to do it but I had to decompile ApplicationConfiguration (which is not even in the API jar) to figure out how to do it.

Since this is a community, you could share your knowledge for everyone stumbling across this thread looking for something similar.

Thanks.

0

Sure Frito,

Here are the highlights....

class ApplicationConfigurationWrapper extends ModuleBasedConfiguration<JavaRunConfigurationModule>
        implements RunJavaConfiguration, SingleClassConfiguration, RefactoringListenerProvider {
    private ApplicationConfiguration delegate;

    public ApplicationConfigurationWrapper(ApplicationConfiguration delegate) {
        super(delegate.getName(), delegate.getConfigurationModule(), delegate.getFactory());
        this.delegate = delegate;
    }


    public RunProfileState getState(@NotNull Executor executor, @NotNull ExecutionEnvironment env) throws ExecutionException {
        JavaCommandLineState state = new MyJavaCommandLineState(env);
        state.setConsoleBuilder(TextConsoleBuilderFactory.getInstance().createBuilder(getProject()));
        return state;
    }


    private class MyJavaCommandLineState extends JavaCommandLineState {
        public MyJavaCommandLineState(ExecutionEnvironment environment) {
            super(environment);
        }

        protected JavaParameters createJavaParameters() throws ExecutionException {
            JavaParameters params = new JavaParameters();
            params.setupEnvs(delegate.getEnvs(), delegate.PASS_PARENT_ENVS);
            int classPathType = JavaParametersUtil.getClasspathType(delegate.getConfigurationModule(), delegate.MAIN_CLASS_NAME, false);
            JavaParametersUtil.configureModule(delegate.getConfigurationModule(), params, classPathType, delegate.ALTERNATIVE_JRE_PATH_ENABLED ? delegate.ALTERNATIVE_JRE_PATH : null);
            JavaParametersUtil.configureConfiguration(params, delegate);
            // here I inject my parameters
            params.getVMParametersList().add("myParameter1");
            params.getVMParametersList().add("myParameter2");
            params.getVMParametersList().add("myParameter3");

            // here I set my own main class
            params.setMainClass(MyMainClass.class.getName());
            for (RunConfigurationExtension ext : (RunConfigurationExtension[]) Extensions.getExtensions(RunConfigurationExtension.EP_NAME)) {
                ext.updateJavaParameters(delegate, params, getRunnerSettings());
                ParametersList parametersList = params.getVMParametersList();
            }

            // Here I added something to the path
            params.getClassPath().add(PathUtil.getJarPathForClass(getClass()));
            return params;
        }

        protected OSProcessHandler startProcess() throws ExecutionException
        {
            OSProcessHandler handler = super.startProcess();
            for (RunConfigurationExtension ext : (RunConfigurationExtension[])Extensions.getExtensions(RunConfigurationExtension.EP_NAME)) {
                ext.handleStartProcess(delegate, handler);
            }

            return handler;
        }
    }

}



Here is the code to launch the current Configuration with my modifications:

    public void actionPerformed(AnActionEvent e) {
        ProjectManager projectManager = ProjectManager.getInstance();
        Project[] openProjects = projectManager.getOpenProjects();
        if (openProjects.length==0) {return;}
        Project project = openProjects[0];
        RunManager runManager = RunManager.getInstance(project);
        if (!( runManager.getSelectedConfiguration().getConfiguration() instanceof ApplicationConfiguration)) {
            return;
        }
        Executor executor = DefaultRunExecutor.getRunExecutorInstance();
        RunnerAndConfigurationSettingsImpl selectedConfiguration = new RunnerAndConfigurationSettingsImpl(
                (RunManagerImpl) runManager,
               new ApplicationConfigurationWrapper((ApplicationConfiguration) runManager.getSelectedConfiguration().getConfiguration()),
                runManager.getSelectedConfiguration().isTemplate()
        );


        ProgramRunner runner = RunnerRegistry.getInstance().getRunner(executor.getId(),selectedConfiguration.getConfiguration());
        ExecutionEnvironment environment = new ExecutionEnvironment(runner, selectedConfiguration, e.getDataContext());

        try {
            runner.execute(executor, environment);
        } catch (ExecutionException e1) {
            JavaExecutionUtil.showExecutionErrorMessage(e1, "Error", project);
        }
    }

I hope this helps...
0

Please sign in to leave a comment.