25

For some unknown reason, whenever I run gradle clean build after I start my notebook it works as expected. When I try for the second time, I always get

Unable to delete directory 'C:\_d\mycompany\WSs\demo\build'
  Failed to delete some children. This might happen because a process has files open or has its working directory set in the target directory.
  - C:\_d\mycompany\WSs\demo\build\classes\kotlin\main\com\mybank
  - C:\_d\mycompany\WSs\demo\build\classes\kotlin\main\com
  - C:\_d\mycompany\WSs\demo\build\classes\kotlin\main
  - C:\_d\mycompany\WSs\demo\build\classes\kotlin\test\com
  - C:\_d\mycompany\WSs\demo\build\classes\kotlin\test
  - C:\_d\mycompany\WSs\demo\build\classes\kotlin
  - C:\_d\mycompany\WSs\demo\build\classes
  - C:\_d\mycompany\WSs\demo\build\generated\source\kapt
  - C:\_d\mycompany\WSs\demo\build\generated\source\kaptKotlin\main
  - C:\_d\mycompany\WSs\demo\build\generated\source\kaptKotlin\test
  - C:\_d\mycompany\WSs\demo\build\generated\source\kaptKotlin
  - C:\_d\mycompany\WSs\demo\build\generated\source
  - C:\_d\mycompany\WSs\demo\build\generated
  - C:\_d\mycompany\WSs\demo\build\kotlin\compileKotlin\caches-jvm\inputs
  - C:\_d\mycompany\WSs\demo\build\kotlin\compileKotlin\caches-jvm\jvm\kotlin
  - C:\_d\mycompany\WSs\demo\build\kotlin\compileKotlin\caches-jvm\jvm
  - and more ...
  New files were found. This might happen because a process is still writing to the target directory.
  - C:\_d\mycompany\WSs\demo\build\kotlin\compileKotlin

I found someone failing quite similar problem but using Android Studio/Ubuntu (I am using IntelliJ Community/Windows 10): other question

I tried carefully all recommendations and none of them fixed my issue. I only get it working back if I restart my Windows and just work one time.

Some of the recommendations are:

1 - Close the studio and go to the path the issue is located at and delete the folder there. I tried close IntelliJ and I can't delete the folder straight

2 - File > Settings > Build, Execution, Deployment > Instant Run > Uncheck this Check box (Enable Instant Run to hot swap code) There is no such optional in my IntelliJ

IntelliJ IDEA 2020.2.3 (Community Edition)
Build #IC-202.7660.26, built on October 6, 2020
Runtime version: 11.0.8+10-b944.34 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 1963M
Cores: 8
Non-Bundled Plugins: Lombook Plugin, org.jetbrains.kotlin

3 - gradlew clean Same issue

4 - right-click on the directory, click properties on the context menu that will appear, then uncheck the "Read Only" attribute. After that, click the "Apply" to apply changes. I am administrator but I couldn't change such folder attribute.

5 - Running android studio as administrator in windows worked for me. Same issue

Here are my build.gradle

plugins {
    id "org.jetbrains.kotlin.jvm" version "1.4.10"
    id "org.jetbrains.kotlin.kapt" version "1.4.10"
    id "org.jetbrains.kotlin.plugin.allopen" version "1.4.10"
    id "com.github.johnrengelman.shadow" version "6.1.0"
    id "io.micronaut.application" version '1.0.5'
    id "com.gorylenko.gradle-git-properties" version "2.2.2"
}

version "0.1"
group "com.mybank"

repositories {
    mavenCentral()
    jcenter()
}

micronaut {
    runtime "netty"
    testRuntime "junit5"
    processing {
        incremental true
        annotations "com.mybank.*"
    }
}

dependencies {
    implementation("io.micronaut:micronaut-validation")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut:micronaut-runtime")
    implementation("javax.annotation:javax.annotation-api")
    implementation("io.micronaut:micronaut-http-client")

    implementation("io.micronaut:micronaut-management")
    implementation("io.micronaut.sql:micronaut-jdbc-hikari")
    implementation("io.micronaut.sql:micronaut-hibernate-jpa")
    runtimeOnly("com.h2database:h2")
    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")



}

mainClassName = "com.mybank.ApplicationKt"
java {
    sourceCompatibility = JavaVersion.toVersion('11')
}

compileKotlin {
    kotlinOptions {
        jvmTarget = '11'
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = '11'
    }
}

gradle.properties

micronautVersion=2.1.3
kotlinVersion=1.4.10

and the build folder seen in IntelliJ

enter image description here

*** edited

I didn't find how fix it. Now is happening with a project I have just cloned from micronaut

And I can't delete build folder although I am Windows administrator

PS C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin> gradle clean build
> Task :clean FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':clean'.
> java.io.IOException: Unable to delete directory 'C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build'
    Failed to delete some children. This might happen because a process has files open or has its working directory set in 
the target directory.
    - C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build\generated\source\kaptKotlin\main
    - C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build\generated\source\kaptKotlin\test
    - C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build\generated\source\kaptKotlin
    - C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build\generated\source
    - C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build\generated
    - C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build\tmp\kapt3\incApCache\main
    - C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build\tmp\kapt3\incApCache\test
    - C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build\tmp\kapt3\incApCache
    - C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build\tmp\kapt3\stubs\main
    - C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build\tmp\kapt3\stubs\test
    - C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build\tmp\kapt3\stubs
    - C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build\tmp\kapt3
    - C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin\build\tmp

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 13s
1 actionable task: 1 executed
PS C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin> gradle stop

FAILURE: Build failed with an exception.

* What went wrong:
Task 'stop' not found in root project 'hello-world-kotlin'.

* Try:
Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or 
--debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.7/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 2s
PS C:\_d\toLearn\micronaut-grpc\examples\hello-world-kotlin> 
TheInitializer
  • 566
  • 7
  • 20
Jim C
  • 3,957
  • 25
  • 85
  • 162

10 Answers10

29

The problem is that you have multiple Gradle processes that are still holding locks to your files.

You can try running gradle --stop when this happens to stop any daemons that are running in the backgorund (this usually helps).

You can also check the state of your daemons by running gradle --status. For me this outputs:

No Gradle daemons are running.

Only Daemons for the current Gradle version are displayed.
See https://docs.gradle.org/6.5/userguide/gradle_daemon.html#sec:status

If you see something there it might be a problem.

Using --no-daemon and --no-parallel might help, but they would just hide the underlying problem.

In my case when this happened I realized that I had processes running in the background because of tests never finishing. Check all your threading and/or coroutine usage for unclosed / unfinished processes.

One other case when this happened to me is when I was using ProcessBuilder to start another Java process and it didn't exit.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
  • Thanks but it is still happening. Interesting it have never happen before with this same notebook and gradle installation. It started to happen only with micronaut projects – Jim C Nov 11 '20 at 19:25
  • 1
    Then it is probably a Micronaut resource leak that prevents some processes from stopping. – Adam Arold Nov 11 '20 at 21:09
  • No clue at all what is wrong. I was facing this issue with my company laptop which I can't log as administrator. Today I had same issue with my personal laptop which obviously I am adminstrator. I installed a free tool in order to find out which process was blocking this folder and it pointed to java.exe I killed the process and I still couldn't delete the folder. Only after I closed IntelliJ and Visual Studio Code I finally managed to delete the folder. Quite strange issue. BTW, I only have this issue with projects in gradle containing micronaut dependencies. – Jim C Nov 12 '20 at 13:02
  • 1
    Oh, if you have incremental compilation enabled in IDEA it will produce this problem. – Adam Arold Nov 14 '20 at 07:46
  • not help! i have this error yet – Mojtaba Darzi Jan 11 '22 at 04:54
  • This solution not worked for me as well – Devendra Singraul Jun 16 '22 at 16:59
7

In my case, I experienced this error after I ran a Gradle command as the root user in Linux.

After switching back to a regular user, Gradle gave me the above error. When I tried to remove the build directory using rm -rf build/, I received a "Permission denied" error. You can solve this problem by removing the build directory as the root user.

Julia
  • 1,950
  • 1
  • 9
  • 22
5

Just rename the " build " folder.

Hardik Patel
  • 83
  • 1
  • 7
5

In Windows 10 Kill all Java TM instances by running this command on command prompt:

TASKKILL /F /IM java.exe 
desertnaut
  • 57,590
  • 26
  • 140
  • 166
1

For me the files where under a different user and the group and the permissions were not sufficient to write to the files of the directory in question.

Try checking who the user of the project files are with ls -al

If the project and the sub directories and files are under a different user. You can run sudo chown -R [new_owner] [project_folder_name] The flag -R here recursively changes owner for the sub files and sub dirs.

You can also add additional permissions to the group (if the user belongs to the group) or even change the group (if the user does not belong to the group). The basic idea is giving the user enough permission (rwx) to use files.

Robin
  • 904
  • 8
  • 16
1

Or you delete your app installed on your phone and reinstall it again.This solved my problem

0

As I have faced similar kinds of errors during the flutter app development, I have figured out which process is blocking the running app. So I have just ended Open JDK from the Process list and then retried running the app without debug mode and it's working. The thing I learned here is that we have multiple development environments running in the background so we have to determine which process is blocking others.

0

Close all applications. Restart your system. It Works.

starball
  • 20,030
  • 7
  • 43
  • 238
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 22 '23 at 09:17
-1

Delete build folder in this path : your project\app . Then rebuild project.

Javad Shirkhani
  • 343
  • 6
  • 11
-5

I went to my file system and deleted the file manually. Restarted my android