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.

1 Like

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).

1 Like

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


As per me you can go with these steps.

  1. Connect to Gradle: Use the Tooling API to connect to your Gradle project.
  2. Create JavaExec Task: Define and configure a JavaExec task programmatically.
  3. Set Configuration: Specify the main class, JVM arguments, and environment variables.
  4. Execute Task: Run the configured JavaExec task using the Tooling API.
    Thank you

And how would you create and configure the task over the tooling API if not using an init script?
Or did you just paste an AI answer that can never work?