Javexec, error 'No main class specified'


a add this task to my build.gradle file :

task encrypt(type: JavaExec) << {
main = 'it.sad.lib.security.EncryptPassword’
classpath = sourceSets.main.runtimeClasspath
args =‘xx’

Executing this task with gradlew encrypt i get this
message :

  • What went wrong:
    Execution failed for task ‘:encrypt’.

No main class specified

What do i wrong, is main = ‘it.sad.lib.security.EncryptPassword’ not enough ?

remove the ‘<<’ from your task definition and it should work as expected.

The JavaExec task has a task action which executes the Java program. By using <<, you’re adding the configuration of the main, classpath, and args as a task action as well. When the task action provided by the JavaExec runs, the second task action to configure these values has not run yet. You likely want to configure these values in the configuration phase, not in a task action by removing the <<.

Really i prefer to do it in ‘encrypt’ task itself, it’s an isolated task has nothing to do with build.

Here my code :

// encrypt a string
task encrypt(type: JavaExec) {
def string = null
// check if task ‘encrpy’ is called then ask
// for string
gradle.taskGraph.whenReady { taskGraph ->
if (taskGraph.hasTask(encrypt)) {
string = System.console().readLine(" Enter password: “)
main = 'it.sad.lib.security.EncryptPassword’
classpath = sourceSets.main.runtimeClasspath
args = (”$string").toList();


I don’t think you understood the responses to your question. You asked what you did wrong with your original code. Both @Rene and I both mentioned what you did wrong (included <<) at about the same time. In my answer, I also described what your code was actually doing from a Gradle lifecycle and implementation perspective so that you could understand why removing << was the answer.

You quoted “task action which executes the Java program.” Your encrypt task is defined as type JavaExec. Tasks have a sequence of task actions that are executed. JavaExec has one that executes the Java program with the main you specified. That’s just a fact of how it works, not a suggestion. The main point was just that the ordering needed to be correct.