json - Spring Boot can find resource file, but Java class fails with NullPointerException -
i working on project in 1 class need load json
file src/main/resources
.
src/ main/java/firebasetokenverifier main/resources/staging_firebase.json
the class forebasetokenverifier
loads json
file in constructor
@component public class firebasetokenverifier implements tokenverifier { private firebaseauth firebaseauth; // todo (hhimanshu): resourcefile , databaseurl hardcoded, should picked external file // or should picked conditionally based on environment public firebasetokenverifier() throws ioexception, urisyntaxexception { final string resourcename = "staging_firebase.json"; system.out.println("getting resource: " + resourcename); path path = paths.get(classloader.getsystemclassloader().getresource(resourcename).touri()); fileinputstream serviceaccount = new fileinputstream(path.tofile()); firebaseoptions options = new firebaseoptions.builder() .setcredential(firebasecredentials.fromcertificate(serviceaccount)) .setdatabaseurl("https://staging-myapp.firebaseio.com") .build(); firebaseapp defaultapp = firebaseapp.initializeapp(options); firebaseauth = firebaseauth.getinstance(defaultapp); } /// more things here ..... }
when run mvn spring-boot:run
, see json
file loaded correctly
2017-07-26 15:57:33.611 info 49104 --- [ost-startstop-1] o.a.c.c.c.[tomcat].[localhost].[/] : initializing spring embedded webapplicationcontext 2017-07-26 15:57:33.611 info 49104 --- [ost-startstop-1] o.s.web.context.contextloader : root webapplicationcontext: initialization completed in 1424 ms getting resource: staging_firebase.json 2017-07-26 15:57:33.799 info 49104 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean : mapping filter: 'characterencodingfilter' to: [/*]
and works fine. then, try following
mvn clean package; java -jar target/myapp-core-0.0.1-snapshot.jar
but fails on runtime
at org.springframework.beans.factory.support.constructorresolver.autowireconstructor(constructorresolver.java:189) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.autowireconstructor(abstractautowirecapablebeanfactory.java:1193) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:1095) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:513) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:483) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:202) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.config.dependencydescriptor.resolvecandidate(dependencydescriptor.java:208) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1138) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:1066) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.constructorresolver.resolveautowiredargument(constructorresolver.java:835) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:741) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] ... 35 common frames omitted caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'firebasetokenverifier' defined in url [jar:file:/users/harit.himanshu/bl/sources/idea/myapp-core/targ et/myapp-core-0.0.1-snapshot.jar!/boot-inf/classes!/com/myapp/api/auth/firebasetokenverifier.class]: instantiation of bean failed; nested exception org.springframework.beans.beaninstantiationex ception: failed instantiate [com.myapp.api.auth.firebasetokenverifier]: constructor threw exception; nested exception java.lang.nullpointerexception @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instantiatebean(abstractautowirecapablebeanfactory.java:1155) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:1099) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:513) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:483) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:202) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.config.dependencydescriptor.resolvecandidate(dependencydescriptor.java:208) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1138) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:1066) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.constructorresolver.resolveautowiredargument(constructorresolver.java:835) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:741) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] ... 49 common frames omitted caused by: org.springframework.beans.beaninstantiationexception: failed instantiate [com.myapp.api.auth.firebasetokenverifier]: constructor threw exception; nested exception java.lang.nullpointe rexception @ org.springframework.beans.beanutils.instantiateclass(beanutils.java:154) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.simpleinstantiationstrategy.instantiate(simpleinstantiationstrategy.java:89) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instantiatebean(abstractautowirecapablebeanfactory.java:1147) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] ... 61 common frames omitted caused by: java.lang.nullpointerexception: null @ com.myapp.api.auth.firebasetokenverifier.<init>(firebasetokenverifier.java:31) ~[classes!/:0.0.1-snapshot] @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) ~[na:1.8.0_131] @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:62) ~[na:1.8.0_131] @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:45) ~[na:1.8.0_131] @ java.lang.reflect.constructor.newinstance(constructor.java:423) ~[na:1.8.0_131] @ org.springframework.beans.beanutils.instantiateclass(beanutils.java:142) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] ... 63 common frames omitted
it fails on l31
path path = paths.get(classloader.getsystemclassloader().getresource(resourcename).touri());
i confirm json
file present in jar file
✗ jar -tvf target/myapp-core-0.0.1-snapshot.jar| grep json 2355 wed jul 26 16:00:44 nzst 2017 boot-inf/classes/staging_firebase.json 64952 tue jan 07 19:29:24 nzdt 2014 boot-inf/lib/json-20140107.jar
what going wrong here? , how come spring-boot
able resolve path correctly?
update
after looking @ https://stackoverflow.com/a/36372773/379235, changed code
fileinputstream serviceaccount = new fileinputstream(new classpathresource(resourcename).getfile());
and fails saying
caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'firebasetokenverifier' defined in url [jar:file:/users/harit.himanshu/bl/sources/idea/myapp-core/target/myapp-core-0.0.1-snapshot.jar!/boot-inf/classes!/com/myapp/api/auth/firebasetokenverifier.class]: instantiation of bean failed; nested exception org.springframework.beans.beaninstantiationex ception: failed instantiate [com.myapp.api.auth.firebasetokenverifier]: constructor threw exception; nested exception java.io.filenotfoundexception: class path resource [staging_firebase.json] cannot resolved absolute file path because not reside in file system: jar:file:/users/harit.himanshu/bl/sources/idea/myapp-core/target/myapp-core-0.0.1-snapshot.jar!/boo t-inf/classes!/staging_firebase.json @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instantiatebean(abstractautowirecapablebeanfactory.java:1155) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:1099) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:513) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:483) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:202) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.config.dependencydescriptor.resolvecandidate(dependencydescriptor.java:208) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1138) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:1066) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.constructorresolver.resolveautowiredargument(constructorresolver.java:835) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:741) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] ... 49 common frames omitted caused by: org.springframework.beans.beaninstantiationexception: failed instantiate [com.myapp.api.auth.firebasetokenverifier]: constructor threw exception; nested exception java.io.filenotfoundexception: class path resource [staging_firebase.json] cannot resolved absolute file path because not reside in file system: jar:file:/users/harit.himanshu/bl/sources/idea/penn ytrak-core/target/myapp-core-0.0.1-snapshot.jar!/boot-inf/classes!/staging_firebase.json @ org.springframework.beans.beanutils.instantiateclass(beanutils.java:154) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.simpleinstantiationstrategy.instantiate(simpleinstantiationstrategy.java:89) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instantiatebean(abstractautowirecapablebeanfactory.java:1147) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] ... 61 common frames omitted caused by: java.io.filenotfoundexception: class path resource [staging_firebase.json] cannot resolved absolute file path because not reside in file system: jar:file:/users/harit.himanshu/bl/sources/idea/myapp-core/target/myapp-core-0.0.1-snapshot.jar!/boot-inf/classes!/staging_firebase.json @ org.springframework.util.resourceutils.getfile(resourceutils.java:215) ~[spring-core-4.3.8.release.jar!/:4.3.8.release] @ org.springframework.core.io.abstractfileresolvingresource.getfile(abstractfileresolvingresource.java:52) ~[spring-core-4.3.8.release.jar!/:4.3.8.release] @ com.myapp.api.auth.firebasetokenverifier.<init>(firebasetokenverifier.java:34) ~[classes!/:0.0.1-snapshot] @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) ~[na:1.8.0_131] @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:62) ~[na:1.8.0_131] @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:45) ~[na:1.8.0_131] @ java.lang.reflect.constructor.newinstance(constructor.java:423) ~[na:1.8.0_131] @ org.springframework.beans.beanutils.instantiateclass(beanutils.java:142) ~[spring-beans-4.3.8.release.jar!/:4.3.8.release] ... 63 common frames omitted
even though can see in jar file
✗ jar -tvf target/myapp-core-0.0.1-snapshot.jar| grep json 2355 wed jul 26 16:54:02 nzst 2017 boot-inf/classes/staging_firebase.json 64952 tue jan 07 19:29:24 nzdt 2014 boot-inf/lib/json-20140107.jar
cannot resolved absolute file path because not reside in file system
this pretty explicit. you're trying access file, when not file resource embedded in jar.
research classloader#getresourceasstream()
.
Comments
Post a Comment