Just put it in the runtime classpath and use ClassLoader#getResourceAsStream()
to get an InputStream
of it. Putting it in the JAR file among the classes, or adding its (JAR-relative) path to the Class-Path
entry of the JAR's manifest.mf
file is more than sufficient.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("config/info.txt");
// Do your thing to read it.
Or if you actually want to get it in flavor of a java.io.File
, then make use of ClassLoader#getResource()
, URL#toURI()
and the File
constructor taking an URI
:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("config/info.txt");
File file = new File(url.toURI());
// Do your thing with it.
Do not use relative paths in java.io
stuff. It would be dependent on the current working directory which you have no control over at any way. It's simply receipt for portability trouble. Just make use of the classpath.
That said, are you aware of the java.util.Properties
API? It namely look like you're trying to achieve the same thing which is more easy to be done with propertiesfiles.