Run Gradle task from Java Android

Need some help with an error for Java.
So far I’ve created a gradle task that runs a ruby script using JrubyExec.
When I go to terminal and run "gradlew “task” it works.

I’m now trying to execute that gradle task through the gradle-tooling-api.
I’ve added the gradle-tooling-api-4.6-rc-2.jar file into the library and in my class I have the following which I believe is correct for the gradle documentation.

public void button(View view) {

    ProjectConnection connection = GradleConnector.newConnector()
            .forProjectDirectory(new File("C:/Users/user/AndroidStudio/core/"))
    try {
    } finally {

When I run the app and install the APK onto an emulator and click on the button “button” I get the following error.

Caused by: java.lang.IllegalArgumentException: Could not determine java version from ‘0’.
I’m running the following java version.

java version "1.8.0_151"
Java™ SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot™ 64-Bit Server VM (build 25.151-b12, mixed mode)

Thanks in advance.