java - getResourceAsStream returns null for txt files but valid for images -
platform: java swt using eclipse ide
before continuing, did see similar issue here on stack overflow, 19035407.
source:
package prjmyapp; import java.io.inputstream; public class myapp { public static void main(string[] args) { inputstream ostream = myapp.class.getresourceasstream("resources/startreklandrurocks.txt"); if (null == ostream) system.out.println("input stream null"); else system.out.println("input stream not null :-)"); } } document placement
prjmyapp / src / prjmyapp --> .java files --> resources --> non-image resources --> images --> image resources (jpg, png) interesting note
i used utilize /prjmyapp/resources/whatever.txt , /prjmyapp/images/whatever.ext, during testing figure out problem, realized relative path works , improve visually , otherwise, hence "resources/startreklandrurocks.txt" instead of previous "/prjmyapp/resources/startreklandrurocks.txt". stated, images, , have quite few, work expected on machines (even mac), not text file.
development machine:
my development machine executes code no matter do.
centos box
my centos (linux) box executes self contained inclusive jar file, utilize ant create file. can load text file, if have text file png extension, cannot load file txt extension, same else.
yes, know renaming txt file png extension ought never done, curious happen, can load images (jpg , png) no issues. found out getsystemclassloader not txt extension. renaming extension png hack , fakes method working whatever reason.
i prefer not hack.
why getsystemclassloader not work straight txt extension? according other post should, not on system.
what fix?
the comments led me answer.
the method, getresourceasstream(), worked. did not realize file not in jar txt extension, hence failure.
<target name="jarresources" depends="jarcode"> <jar destfile="${jar.code}" update="true"> <fileset dir="${src.dir}" includes="**/*.png" /> <fileset dir="${src.dir}" includes="**/*.jpg" /> <fileset dir="${src.dir}" includes="**/*.ico" /> <fileset dir="${src.dir}" includes="**/*.txt" /> </jar> </target> i missing lastly line in jarresources section. had png, jpg, , ico originally. adding in txt line resolved issue.
issues can in build script, not in java, lesson learnt. thanks.
java linux swt inputstream
No comments:
Post a Comment