# Run Gradle task from Java Android

**URL:** <https://discuss.gradle.org/t/run-gradle-task-from-java-android/26209>\
**Category:** Help/Discuss\
**Created:** [March 13, 2018, 5:37am UTC](https://discuss.gradle.org/t/run-gradle-task-from-java-android/26209 "2018-03-13T05:37:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![canadiankenny](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@canadiankenny](https://discuss.gradle.org/u/canadiankenny)\
**Post date:** [March 13, 2018, 5:37am UTC](https://discuss.gradle.org/t/run-gradle-task-from-java-android/26209/1 "2018-03-13T05:37:50Z")

</div>

Hi,  
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 MainActivity.java 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/"))
            .connect();
    try {
        connection.newBuild().forTasks("runtask").run();
    } finally {
        connection.close();
    }

```

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.

J
