Run main class from Tooling API

Is there a way to use the Tooling API to run a main class?

To run tests I can use ProjectConnection#newTestLauncher#run() and I can specify classes/methods and jvm args and environment variables. But I can’t see a similar way to run a main class.

The only way of doing this seems to me to have the Application plugin installed but then that would target a specific main class and I wouldn’t be able to override the name, just run that specific task.

Unless it’s possible to use the Tooling API to create a JavaExec task on the fly and run it? But I can’t see a way to do that either.

If you use the application plugin, noone says the main class you configure has to be hard-coded, you can for example configure it from a project property.

But just for a run task applying the application plugin is a bit overkill and you could just register an according JavaExec task yourself.

But both of course would require you do that in the target project.

What you actually want is, to write an init script that adds the task you need to the project and then make sure that init script is used when invoking that task through the tooling API for example by using withArguments("-I", theinitscript).

Clever. I didn’t think of supplying a custom init script. Thanks

1 Like