
|
Why cant my java jar find resource files when running?
Dear Bobajob
I'm currently banging my head against the exact same brick wall. Did you ever solve this problem?
Anyone else?
Many Thanks
|

|
Why cant my java jar find resource files when running?
i think the problem is located on Ant or,more precisely, on the interaction between it and NetBeans.
I've had the same problem working on my thesys,and i've looked all the morning long for a solution.
I can't explain how to avoid this problem forever but keeping in your actual project you can write down a pretty simple solution
Ant uses mostly 2 files: build.xml(in your project main folder) and build-impl.xml (in your project main folder/nbproject)
Open the latter and find the section
<target name="-pre-init">
<!-- Empty placeholder for easier customization. -->
<!-- You can override this target in the ../build.xml file. -->
</target>
add in the midst of the <target> </target> tag a command like
<copy todir="destDirectory">
<fileset dir="sourceDirectory"/>
</copy>
the meaning of the name of the directory is obvious.
Just put a lil attention on inserting the right path:
the jar file to distribute is the one in the /dist folder and it pretend to file the resource file in the same position as the jar file in the main directory do.
I hope i'm been useful
|

|
Why cant my java jar find resource files when running?
i think the problem is located on Ant or,more precisely, on the interaction between it and NetBeans.
I've had the same problem working on my thesis,and i've looked all the morning long for a solution.
I can't explain how to avoid this problem forever but keeping in your actual project you can write down a pretty simple solution
Ant uses mostly 2 files: build.xml(in your project main folder) and build-impl.xml (in your project main folder/nbproject)
Open the latter and find the section
<target name="-pre-init">
<!-- Empty placeholder for easier customization. -->
<!-- You can override this target in the ../build.xml file. -->
</target>
add in the midst of the <target> </target> tag a command like
<copy todir="destDirectory">
<fileset dir="sourceDirectory"/>
</copy>
the meaning of the name of the directory is obvious.
This copy all the file in the source directory inside the destination one.
I've noticed that you have to create it on your own: Ant doesn't do that job
Just put a lil attention on inserting the right path:
the jar file to distribute is the one in the /dist folder and it pretend to file the resource file in the same position as the jar file in the main directory do.
I hope i'm been useful
|

|
Why cant my java jar find resource files when running?
Dear Marco
Many thanks for the work-around - but hacking the xml sounded a bit scary.
As it happens, I found another way. This seems to work for both Netbeans and as a .jar
Code:
InputStream stream = SoloWarforEdadhOpponentApp.getApplication().getClass().getResourceAsStream("resources/MasteryCardData.esd");
InputStreamReader fileReader = new InputStreamReader(stream);
BufferedReader reader = new BufferedReader(fileReader);
Many thanks to Henry Wong on the Javaranch forum for pointing out that the File object was really for accessing the wider file system and not resources, and pointing me towards the getResourceAsStream and leading me to the fact that a BufferedReader doesn't actually need a FileReader object as its argument, just a Reader object.
I hope that helps you too.
As Ever
Simon*
|