Ivajlo Iliev
Ivajlo Iliev

Reputation: 463

java : Copying file from one machine to another throws java.nio.file.NoSuchFileException

I have to copy a file from one windows machine to other (the target is a virtual machine). When I execute it as a normal user, the file is successfully copied and : File f = new File(source); System.out.println(f.exists());

outputs true.
When I open a cmd as an administrator, and run the same program, this piece of code returns false`. And when trying

java.nio.files.Files.copy(source, target, options);

I get the following exception java.nio.file.NoSuchFileException... And this is in the case, when the folder on the remote machine, where the file is, is mapped to a drive, like

source = "N:\\..."

Does anybody have an idea what is the reason? (or respectfully has a solution)?

Upvotes: -1

Views: 1127

Answers (2)

Cybran
Cybran

Reputation: 2313

Your problem is completely unrelated to java, it is related to how windows manages mapped network drives under different privilege levels.

To be precise: Network drive mappings are lost when switching to an elevated user level.

In order to fix it, you have to enable linked connections for network drives. When you are running Windows 7: In the registry editor, create a DWORD with then name EnableLinkedConnections of value 1 under HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Policies/System.

A detailed explanation can be found here: http://www.winability.com/how-to-make-elevated-programs-recognize-network-drives/

Upvotes: 2

1 not the same system

2 not the same drive

3 not exactly the same paths

4 you dont launch from the same path

A way to fix: before read a file, write another, and check where it is written in your filesystem

Upvotes: 0

Related Questions