From 4161eb1853211bc488b449897ba46ef02c6bd0fe Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 20 Oct 2023 09:15:54 +0100 Subject: [PATCH] Fix path handling in NestedLocation on Windows See 4b495ca See gh-37668 --- .../loader/net/protocol/nested/NestedLocation.java | 12 ++++++++++-- .../net/protocol/nested/NestedLocationTests.java | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/nested/NestedLocation.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/nested/NestedLocation.java index c37555dec87..07e304048e6 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/nested/NestedLocation.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/nested/NestedLocation.java @@ -16,6 +16,7 @@ package org.springframework.boot.loader.net.protocol.nested; +import java.io.File; import java.net.URI; import java.net.URL; import java.nio.file.Path; @@ -100,9 +101,16 @@ public record NestedLocation(Path path, String nestedEntryName) { } private static NestedLocation create(int index, String location) { - String path = location.substring(0, index); + String locationPath = location.substring(0, index); + if (isWindows() && locationPath.startsWith("/")) { + locationPath = locationPath.substring(1, locationPath.length()); + } String nestedEntryName = location.substring(index + 2); - return new NestedLocation((!path.isEmpty()) ? Path.of(path) : null, nestedEntryName); + return new NestedLocation((!locationPath.isEmpty()) ? Path.of(locationPath) : null, nestedEntryName); + } + + private static boolean isWindows() { + return File.separatorChar == '\\'; } static void clearCache() { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/net/protocol/nested/NestedLocationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/net/protocol/nested/NestedLocationTests.java index 5f4314de44d..b71dc266be3 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/net/protocol/nested/NestedLocationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/net/protocol/nested/NestedLocationTests.java @@ -120,7 +120,7 @@ class NestedLocationTests { void fromUriReturnsNestedLocation() throws Exception { File file = new File(this.temp, "test.jar"); NestedLocation location = NestedLocation - .fromUri(new URI("nested:" + file.getAbsolutePath() + "/!lib/nested.jar")); + .fromUri(new URI("nested:" + file.getAbsoluteFile().toURI().getPath() + "/!lib/nested.jar")); assertThat(location.path()).isEqualTo(file.toPath()); assertThat(location.nestedEntryName()).isEqualTo("lib/nested.jar"); }