Inside my resources folder I have a folder called init. I want to copy that folder and everything inside of it to the outside of the jar in a folder called ready. And I want to do that without using any external libraries, just pure java.
I have tried the following
public static void copyFromJar(String source, final Path target)
throws
URISyntaxException,
IOException
{
URI resource = ServerInitializer.class.getResource("").toURI();
FileSystem fileSystem = FileSystems.newFileSystem(resource, Collections.<String, String>emptyMap());
final Path jarPath = fileSystem.getPath(source);
Files.walkFileTree(jarPath, new SimpleFileVisitor<>()
{
private Path currentTarget;
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws
IOException
{
currentTarget = target.resolve(jarPath.relativize(dir).toString());
Files.createDirectories(currentTarget);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws
IOException
{
Files.copy(file, target.resolve(jarPath.relativize(file).toString()),
StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
}
However my application already dies at line
FileSystem fileSystem = FileSystems.newFileSystem(resource, Collections.<String, String>emptyMap());
with exception
java.lang.IllegalArgumentException: Path component should be '/'
when I call
copyFromJar("/init", Paths.get("ready");
Any idea what I am doing wrong? Or can someone provide me code to copy directory from jar to outside of it without using any external libraries?
Just for reference I already looked at this solution but it is too old and uses apache library but I need pure java solution that works both on windows and linux.