java - Access resource folder within jar -
i trying read contents of resource
folder once jar built. resource folder marked source
in ide settings (intellij).
i have tried following methods:
inputstream input = getclass().getresourceasstream("../objectlocation.json"); jsonreader jsonreader = new jsonreader(new inputstreamreader(input));
i have tried:
jsonreader jsonreader = new jsonreader(new filereader("../resources/objectlocation.json"));
both of these methods results in :
which results in:
java.io.filenotfoundexception: com/layers/resources/objectlocation.json (no such file or directory)
file structure:
src -com.layers -> myclasses -resources -> json
edit:
inputstream input = getclass().getresourceasstream("objectlocation.json"); jsonreader jsonreader = new jsonreader(new inputstreamreader(input));
results in a:
java.lang.nullpointerexception
not understanding difference between absolute , relative paths when loading resources in java via getresourceasstream()
common source of errors leading nullpointerexception
.
assuming following structure , content:
my project |-src |-main |-java | |-somepackage | |-someclass.java |-resources |-root.txt |-somepackage |-mydata.txt |-somepackage2 |-mysubdata.txt
content re-organized following in .jar:
|-root.txt |-somepackage |-someclass.java |-mydata.txt |-somepackage2 |-mysubdata.txt
the following indicates works , not work retrieve resource data:
inputstream is; = someclass.class.getresourceasstream("root.txt"); // not ok = someclass.class.getresourceasstream("/root.txt"); // ok = someclass.class.getresourceasstream("/mydata.txt"); // not ok = someclass.class.getresourceasstream("mydata.txt"); // ok = someclass.class.getresourceasstream("/somepackage/mydata.txt"); // ok = someclass.class.getresourceasstream("somepackage/mydata.txt"); // not ok = someclass.class.getresourceasstream("mysubdata.txt"); // not ok = someclass.class.getresourceasstream("somepackage/somepackage2/mysubdata.txt"); // ok = someclass.class.getresourceasstream("/somepackage/somepackage2/mysubdata.txt"); // not ok = someclass.class.getresourceasstream("/somepackage2/mysubdata.txt"); // not ok = someclass.class.getresourceasstream("somepackage2/mysubdata.txt"); // ok
getresourceasstream()
operates relative package corresponding called class instance.
Comments
Post a Comment