How to run a task which type is JavaExec?


(Yekta Sarioglu) #1

Hello there, I would like to share my problem. It gives me creeps to think how can I solve my problem. Because I tried a lot of different approaches. My problem is I want to run a task which type is JavaExec. And my Java terminal program is inside of a different module and takes 7 arguments. How can I run this task in my app’s gradle? I tried a different approach below before I learn that you can execute Java programs within a task in Gradle.

task runIt(type: Exec){
workingDir='C:/dev/workspace/android/utopicminds/texture_atlas_generator/src/main/java/com/example/yekta/texture_atlas_generator'
 commandLine 'cmd /c java -classpath "C:/dev/workspace/android/utopicminds/texture_atlas_generator/src/main/java" com.example.yekta.texture_atlas_generator.AtlasGenerator atlas 2048 2048 0 0 0 ../../../../../../../../app/src/main/assets/images/common'
}

But it gave me an error in line where commandLine property resides. Something like; there is no such directory. And finally, I tried JavaExec like below.

    task execute(type:JavaExec){
    classpath='C:/dev/workspace/android/utopicminds/texture_atlas_generator/src/main/java/com/example/yekta/texture_atlas_generator'
    main=AtlasGenerator
   args 'atlas', '2048', '2048', '0', '0', '0', '../../../../../../../../app/src/main/assets/images/common'}

And I gave me an error like;
Cannot cast object ‘C:/dev/workspace/android/utopicminds/texture_atlas_generator/src/main/java/com/example/yekta/texture_atlas_generator’ with class ‘java.lang.String’ to class ‘org.gradle.api.file.FileCollection’

And also, that Java program is inside of a different module.

How can I implement my idea? Thanks in advance.


(Stefan Wolf) #2

Hi @yektasarioglu,

I guess you should wrap the argument for classpath into a call to files, converting the String into a FileCollection:

classpath = files(C:/dev/workspace/android/utopicminds/texture_atlas_generator/src/main/java/com/example/yekta/texture_atlas_generator')

Even if you do that, I am pretty sure you don’t want to use the source directory as the classpath, right? Do you maybe want the compiled classes? Here are some instructions how to do so: http://mrhaki.blogspot.com/2010/09/gradle-goodness-run-java-application.html

You may want to try the application plugin, which provides a run task, too: https://docs.gradle.org/current/userguide/application_plugin.html#sec:application_tasks

Cheers,
Stefan