简体   繁体   中英

Spring boot - codeSource is returning as null

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

Try

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

getClassLoader() is the key in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM