From 73eb84f9c2a8e4502823077752ba3f7b02526dac Mon Sep 17 00:00:00 2001 From: Paul Schwarz Date: Fri, 15 May 2020 09:29:45 +0200 Subject: [PATCH] Make classpath robustly platform independent https://stackoverflow.com/a/17870390/2694806 --- .../java/com/overstock/sample/processor/Compiler.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/overstock/sample/processor/Compiler.java b/src/test/java/com/overstock/sample/processor/Compiler.java index ea481e0..2e2472b 100644 --- a/src/test/java/com/overstock/sample/processor/Compiler.java +++ b/src/test/java/com/overstock/sample/processor/Compiler.java @@ -87,7 +87,15 @@ private static String buildClassPath(File outputDir) { } private static String classPathFor(Class clazz) { - return clazz.getProtectionDomain().getCodeSource().getLocation().getFile(); + URL url = clazz.getProtectionDomain().getCodeSource().getLocation(); + + try { + URI uri = url.toURI(); + + return Paths.get(uri).toFile().getAbsolutePath(); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } } protected File writeSourceFile(SourceFile sourceFile) throws IOException {