java code snippet to read file before and after packaging into jar
i know this must have been answered a million times, but i can't seem to find it anywhere. So here goes: in my junit test I have this to access the file foo.txt
:
this.ge开发者_开发知识库tClass().getResourceAsStream("/foo.txt")
this works fine until I package it into a jar file. how should i edit my code so that it works in both cases?
thanks a lot
Check that your foo.txt is packaged in root of your jar file. I think that this file is probably present into file system, so when you are running the code from IDE it works, but for some reason it is not in jar.
Possible reason is if you are creating jar using ant script and configured the include pattern as *.class.
Make sure the text file is packaged in the jar:
test.jar =>
-> META-INF/MANIFEST.MF
-> com/somepackage/ClassThatLoadsFooTxt.class
-> foo.txt
If the class file and foo.txt
is contained in the src folder like this:
-> src/com/somepackage/ClassThatLoadsFooTxt.class
-> src/foo.txt
make sure the Class-Path
entry in your MANIFEST.MF points to src:
Class-Path: src/
精彩评论