Skip to content

Commit 8e84151

Browse files
author
Dave Syer
committed
Normalize search path in PropertiesLauncher
* Windows: allow absolute file paths without file:/// prefix * All: only add nested archives (not directories), so loader.path=lib/* behaves the same as -classpath=lib/* (except for adding zip files) Fixes gh-1352
1 parent ccfc470 commit 8e84151

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/PropertiesLauncher.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ private List<Archive> getClassPathArchives(String path) throws Exception {
456456
String root = cleanupPath(stripFileUrlPrefix(path));
457457
List<Archive> lib = new ArrayList<Archive>();
458458
File file = new File(root);
459-
if (!root.startsWith("/")) {
459+
if (!isAbsolutePath(root)) {
460460
file = new File(this.home, root);
461461
}
462462
if (file.isDirectory()) {
@@ -479,6 +479,11 @@ private List<Archive> getClassPathArchives(String path) throws Exception {
479479
return lib;
480480
}
481481

482+
private boolean isAbsolutePath(String root) {
483+
// Windows contains ":" others start with "/"
484+
return root.contains(":") || root.startsWith("/");
485+
}
486+
482487
private Archive getArchive(File file) throws IOException {
483488
String name = file.getName().toLowerCase();
484489
if (name.endsWith(".jar") || name.endsWith(".zip")) {
@@ -534,6 +539,10 @@ private URL[] getURLs(ClassLoader classLoader) {
534539

535540
private String cleanupPath(String path) {
536541
path = path.trim();
542+
// No need for current dir path
543+
if (path.startsWith("./")) {
544+
path = path.substring(2);
545+
}
537546
if (path.toLowerCase().endsWith(".jar") || path.toLowerCase().endsWith(".zip")) {
538547
return path;
539548
}
@@ -542,14 +551,10 @@ private String cleanupPath(String path) {
542551
}
543552
else {
544553
// It's a directory
545-
if (!path.endsWith("/")) {
554+
if (!path.endsWith("/") && !path.equals(".")) {
546555
path = path + "/";
547556
}
548557
}
549-
// No need for current dir path
550-
if (path.startsWith("./")) {
551-
path = path.substring(2);
552-
}
553558
return path;
554559
}
555560

@@ -593,8 +598,7 @@ private static final class ArchiveEntryFilter implements EntryFilter {
593598

594599
@Override
595600
public boolean matches(Entry entry) {
596-
return entry.isDirectory() || entry.getName().endsWith(DOT_JAR)
597-
|| entry.getName().endsWith(DOT_ZIP);
601+
return entry.getName().endsWith(DOT_JAR) || entry.getName().endsWith(DOT_ZIP);
598602
}
599603
}
600604

spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ public void testUserSpecifiedJarPath() throws Exception {
112112
waitFor("Hello World");
113113
}
114114

115+
@Test
116+
public void testUserSpecifiedJarPathWithDot() throws Exception {
117+
System.setProperty("loader.path", "./jars/app.jar");
118+
System.setProperty("loader.main", "demo.Application");
119+
PropertiesLauncher launcher = new PropertiesLauncher();
120+
assertEquals("[jars/app.jar]", ReflectionTestUtils.getField(launcher, "paths")
121+
.toString());
122+
launcher.launch(new String[0]);
123+
waitFor("Hello World");
124+
}
125+
115126
@Test
116127
public void testUserSpecifiedClassLoader() throws Exception {
117128
System.setProperty("loader.path", "jars/app.jar");

0 commit comments

Comments
 (0)