This may be an answer...
I was getting exactly the same issue - only with me it worked the first time, but not on subsequent attempts!
I have a method which I call every so often, which uploads copies of a file to different folders on the SFTP server:
private static void copyToSftpLocations(...)
{
[...]
try (FileSystemManager manager = VFS.getManager())
{
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setSessionTimeout(opts, java.time.Duration.ofMillis(sftpSessionTimeout));
SftpFileSystemConfigBuilder.getInstance().setConnectTimeout(opts, java.time.Duration.ofMillis(sftpSessionTimeout));
SftpFileSystemConfigBuilder.getInstance().setPreferredAuthentications(opts, "publickey,keyboard-interactive,password");
SftpFileSystemConfigBuilder.getInstance().setKnownHosts(opts, new File("known_hosts.txt"));
LOG.debug("Attempting to connect to "+sftpServer+" as "+sftpUserName+"...");
try (FileObject remoteServer = manager.resolveFile("sftp://"+URLEncoder.encode(sftpUserName,StandardCharsets.UTF_8.toString())+":"+URLEncoder.encode(sftpPassword,StandardCharsets.UTF_8.toString())+"@"+sftpServer,opts))
{
while (...)
{
String sftpDirName = ...;
String remotePath = "/"+sftpDirName;
try (FileObject remoteFolder = remoteServer.resolveFile(remotePath))
{
if (!remoteFolder.exists())
{
remoteFolder.createFolder();
}
try (FileObject local = manager.resolveFile(theMasterPath.toUri());
FileObject remoteFile = remoteFolder.resolveFile(theMasterPath.getFileName().toString()))
{
remoteFile.copyFrom(local, Selectors.SELECT_SELF);
}
The first time I called the method, everything worked fine - a copy of the file in each folder.
The second & subsequent times I called the method, I got:
org.apache.commons.vfs2.FileSystemException: Could not find file with URI "sftp://<user>:***@<address>" because it is a relative path, and no base URI was provided.
I tried using sftpServer+"/" when creating the FileObject remoteServer, but it made no difference.
I simply could not see why something that worked first didn't continue to work. Furthermore, fundamentally the same code is used elsewhere in my project, and it works every time!
I'm using commons-vfs2 version 2.9.0.
edit: I have just discovered that if I remove the first try-resource and put
FileSystemManager manager = VFS.getManager();
as an explicit call instead, everything works fine...!