1

Fair warning I'm a new to spring boot...

anyone come across a scenario where a null pointer exception is thrown trying to get the codeSource/URL, I've narrowed it down to this bit of code in the spring code base

ProtectionDomain protectionDomain = getClass().getProtectionDomain();
    CodeSource codeSource = protectionDomain.getCodeSource();
    this is line 119: URI location = (codeSource != null) ? codeSource.getLocation().toURI() : null;

here's the stack trace

org.springframework.boot.loader.JarLauncher.main exited with an exception.
java.lang.IllegalStateException: java.lang.NullPointerException
    at org.springframework.boot.loader.ExecutableArchiveLauncher.<init>(ExecutableArchiveLauncher.java:41)
    at org.springframework.boot.loader.JarLauncher.<init>(JarLauncher.java:35)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.kabira.platform.MainWrapper.invokeMain(MainWrapper.java:66)
Caused by: java.lang.NullPointerException
    at org.springframework.boot.loader.Launcher.createArchive(Launcher.java:119)
    at org.springframework.boot.loader.ExecutableArchiveLauncher.<init>(ExecutableArchiveLauncher.java:38)
    ... 7 more
JoeN
  • 51
  • 1
  • 7

1 Answers1

0

Try

getClass().getClassLoader().getClass()
    .getProtectionDomain()
    .getCodeSource()
    .getLocation()
    .toURI()
    .getPath();

getClassLoader() is the key in this case.

  • 1
    To the reader : remember that in the general case, the more you chain method calls, all the more chances are one of them returns null, and the less easy it is to tell what step returned `null` and caused a `NullPointerException`. – Alain BECKER May 25 '22 at 20:51