# Problem with JavaExec and execution phase

**URL:** https://discuss.gradle.org/t/problem-with-javaexec-and-execution-phase/9928
**Category:** Help/Discuss
**Created:** [June 2, 2015, 3:54pm UTC](https://discuss.gradle.org/t/problem-with-javaexec-and-execution-phase/9928 "2015-06-02T15:54:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![NS\_du\_Toit](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@NS\_du\_Toit](https://discuss.gradle.org/u/NS_du_Toit)
#### Post date: [June 2, 2015, 3:54pm UTC](https://discuss.gradle.org/t/problem-with-javaexec-and-execution-phase/9928/1 "2015-06-02T15:54:51Z")

</div>

Hi!

Thank you for taking a moment to look at my question. I replicated my problem in a very small Gradle build file:

```
apply plugin: 'java'
apply plugin: 'application'

defaultTasks = ['COMPILE_AND_RUN']

mainClassName = "TestClassToExecute" // in the default package
sourceSets.main.java.srcDirs = ["./main/java"]
sourceSets.main.resources.srcDirs = ["./main/resources"]

task COMPILE_AND_RUN(type: JavaExec) << {
	main = mainClassName
	classpath sourceSets.main.runtimeClasspath
	classpath configurations.runtime
}

```

And then I get the following error:

```
Execution failed for task ':COMPILE_AND_RUN'.
> No main class specified

```

Any ideas? I initially had the COMPILE\_AND\_RUN task in the configuration phase (without \<\<) and everything worked fine. But then when I wanted to execute other tasks (eg distZip from the ‘application’ plugin), then it would still execute the COMPILE\_AND\_RUN task as it was defined to be executed in the configuration phase. Subsequently I moved the COMPILE\_AND\_RUN task to the execution phase, but now I’m getting this “No main class specified” error…

What am I missing here? Thank you! 🙂

---

<div class="post-metadata">

### Author: ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)
#### Post date: [June 2, 2015, 4:37pm UTC](https://discuss.gradle.org/t/problem-with-javaexec-and-execution-phase/9928/2 "2015-06-02T16:37:53Z")

</div>

When you define your COMPILE\_AND\_RUN task, if you use ‘\<\<’ you basically declare a ‘doLast’ action on the task.  
This is not what you want to do :  
You want to **configure** the JavaExec task you’re creating, by specifying the _main_ attribute and calling (twice) the _classpath_ method.  
So writing

> task COMPILE\_AND\_RUN(type: JavaExec) {  
> main = mainClassName  
> classpath sourceSets.main.runtimeClasspath  
> classpath configurations.runtime  
> }

without the \<\< shall solve your problem.

---

<div class="post-metadata">

### Author: ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)
#### Post date: [June 2, 2015, 9:01pm UTC](https://discuss.gradle.org/t/problem-with-javaexec-and-execution-phase/9928/3 "2015-06-02T21:01:15Z")

</div>

To clarify my answer:  
By looking at the javaexec code and especially the @TaskAction method call hierarchy , the exception is voluntarily thrown when the main class attribute is not set.  
Since you set it during the doLast action of the task, the exception is thrown when the regular action is executed.  
What do you want to achieve exactly, regarding the distZip task? In the end, what tasks do you want to be executed? Why the compile and run task should not be executed when the distZip task is called?
