简体   繁体   English

Files.move(...) 抛出 FileSystemException:进程无法访问该文件,因为它正被另一个进程使用

[英]Files.move(...) throwing FileSystemException: The process cannot access the file be cause it is being used by another process

private File moveFileTo(File usageFile, File target) {
        File movedFile = new File(target.getPath() + File.separator + usageFile.getName());
        try {
            Files.move(usageFile.toPath(), movedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        } catch (Exception e) {
            log.error("Error while moving {} to {}", usageFile, movedFile, e);
            throw new AppException("Cannot move usage file from " + usageFile + " to " + movedFile);
        }
        return movedFile;
    }  

At the line with Files.move throws FileSystemException.在带有 Files.move 的行中抛出 FileSystemException。 I have a bunch of integration tests and 2 of them fail everytime with the following stacktrace:我有一堆集成测试,其中 2 个每次都失败并出现以下堆栈跟踪:

java.nio.file.FileSystemException: C:\Users\ANDREI~1.TAN\AppData\Local\Temp\input\test-xml-usages-file-copy.xml -> C:\Users\ANDREI~1.TAN\AppData\Local\Temp\process\test-xml-usages-file-copy.xml: The process cannot access the file be cause it is being used by another process. java.nio.file.FileSystemException: C:\Users\ANDREI~1.TAN\AppData\Local\Temp\input\test-xml-usages-file-copy.xml -> C:\Users\ANDREI~1.TAN \AppData\Local\Temp\process\test-xml-usages-file-copy.xml:进程无法访问该文件,因为它正被另一个进程使用。

 at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:301) at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287) at java.nio.file.Files.move(Files.java:1395) at com.bisnode.usagemanagement.infrastructure.importer.file.UsageFileImporterService.moveFileTo(UsageFileImporterService.java:122) at com.bisnode.usagemanagement.infrastructure.importer.file.UsageFileImporterService.importUsagesFile(UsageFileImporterService.java:42) at com.bisnode.usagemanagement.infrastructure.importer.file.scheduler.UsageFileImportScheduler.importFiles(UsageFileImportScheduler.java:33) at com.bisnode.usagemanagement.infrastructure.importer.file.scheduler.UsageFileImportScheduler.importUsageFiles(UsageFileImportScheduler.java:23) at com.bisnode.usagemanagement.infrastructure.importer.UsageImportIntegrationTest.testEndToEndImportXmlUsages(UsageImportIntegrationTest.java:119) at com.bisnode.usagemanagement.infrastructure.importer.UsageImportIntegrationTest.testEndToEndImportXmlUsagesFromInputFileWithoutNamespace(UsageImportIntegrationTest.java:114) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686) at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131) at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84) at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115) at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:212) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) at java.util.ArrayList.forEach(ArrayList.java:1259) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) at java.util.ArrayList.forEach(ArrayList.java:1259) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32) at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220) at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188) at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128) at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:150) at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:124) at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345) at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)

I was thinking about a file locking but the thing is, in my tests, the threads are disabled so no concurrency issues are there.我在考虑文件锁定,但事实是,在我的测试中,线程被禁用,因此不存在并发问题。 I have tried not just Files.move but a few more libraries to move the file but all of them failed.我不仅尝试了 Files.move,还尝试了几个库来移动文件,但都失败了。 The code works fine on Mac but it fails on Windows. I think I miss something but I cannot figure what.该代码在 Mac 上运行良好,但在 Windows 上运行失败。我想我错过了一些东西,但我不知道是什么。

UPDATE更新

    public void importUsagesFile(File usageFile) {
    log.info("Received usage file to import {}, size {} bytes", usageFile.getName(), usageFile.length());
    if (!isValidUsageFile(usageFile)) {
        return;
    }

    log.info("Processing file. Move the file {} to process folder", usageFile.getName());
    File usageFileToProcess = moveFileTo(usageFile, usageFileDirectory.getProcess());
    processUsageFile(usageFileToProcess);
}  

This method will call moveFileTo, actually, after calling moveFileTo, will call processUsageFile that has a try-with-resources.这个方法会调用moveFileTo,实际上,在调用moveFileTo之后,会调用有try-with-resources的processUsageFile。 So on short, importUsagesFile calls moveFileTo, then start to do some business logic.所以简而言之,importUsagesFile 调用 moveFileTo,然后开始执行一些业务逻辑。
UPDATE更新
So I've found the problem, in a test case I was using FileReader and therefore the file was locked.所以我发现了问题,在一个测试用例中,我使用的是 FileReader,因此文件被锁定了。 I put it in a try-with-resources and now it works:D.我把它放在 try-with-resources 中,现在它可以工作了:D。

This is a cross-site dupe to https://serverfault.com/q/1966/22361 .这是对https://serverfault.com/q/1966/22361的跨站点欺骗。

Something else is holding those handles open.其他东西使这些手柄保持打开状态。 You may be leaking open files in your own code (did you use try-with-resources everywhere?), or something else invoked as part of your test scripts is conflicting.您可能会在自己的代码中泄漏打开的文件(您是否在所有地方都使用了 try-with-resources?),或者作为测试脚本的一部分调用的其他内容存在冲突。

So you will have to debug it using Process Explorer or a similar tool ( lsof on Linux, for example).因此,您必须使用 Process Explorer 或类似工具(例如lsof on Linux)对其进行调试。 I'd put a breakpoint in your code to catch the condition and then look for the handle using Process Explorer.我会在您的代码中放置一个断点以捕获条件,然后使用 Process Explorer 查找句柄。 This may be hard to catch, but there's really no other way to do it.这可能很难捕捉到,但实际上没有其他方法可以做到。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FileSystemException 进程无法访问该文件,因为它正被另一个进程使用 测试后无法正确清除结果文件 - FileSystemException The process cannot access the file because it is being used by another process Can't correctly clear result files after test 此进程无法访问该文件,因为 KOTLIN 中的另一个进程 (java.nio.file.FileSystemException) 正在使用该文件 - This process cannot access the file because the file is being used by another process (java.nio.file.FileSystemException) in KOTLIN java.nio.file.FileSystemException:进程无法访问该文件,因为它正被另一个进程使用 - java.nio.file.FileSystemException: The process cannot access the file because it is being used by another process java.nio.file.FileSystemException 进程无法访问该文件,因为它正被另一个进程使用 - java.nio.file.FileSystemException The process cannot access the file because it is being used by another process 如何解决 java.nio.file.FileSystemException 进程无法访问该文件,因为它正在被另一个进程使用 - How to resolve java.nio.file.FileSystemException The process cannot access the file because it is being used by another process readAllBytes()。 FileSystemException:该进程无法访问该文件,因为它正在被另一个进程使用 - readAllBytes(). FileSystemException: The process cannot access the file because it is being used by another process java.nio.file.FileSystemException:无法访问该文件,因为它正由另一个进程使用 - java.nio.file.FileSystemException : cannot access the file because it is being used by another process Java 中的 Files.move() 给出了 FilesSystemException 错误,因为文件夹“正在被另一个进程使用”,但事实并非如此 - Files.move() in Java giving a FilesSystemException error because folder is "being used by another process," but it's not java.nio.file.FileSystemException: C:\p12\dummy.p12: 该进程无法访问该文件,因为它正被另一个进程使用 - java.nio.file.FileSystemException: C:\p12\dummy.p12: The process cannot access the file because it is being used by another process java.nio.file.FileSystemException: C:\test.csv -> C:\test2.csv: The process cannot access the file because it is being used by another process in java - java.nio.file.FileSystemException: C:\test.csv -> C:\test2.csv: The process cannot access the file because it is being used by another process in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM