I want to access to get the string path to my resource key files. If I debug the project in eclipse, everything works like a charm but if I export the project to a runnable .jar file – the project can’t access the key files because the path is wrong.
PUBLIC_KEY_PATH = "src/resources/public.key";
PRIVATE_KEY_PATH = "src/resources/private.key";
But how do I get the right path for the runnable .jar file? I don’t want to get the URL or anything else – I want to get the string path.
I hope that someone can help me.
Solution:
public static String PUBLIC_KEY_PATH = "/resources/public.key";
public static String PRIVATE_KEY_PATH = "/resources/private.key";
I’m using an InputStream to read the file content:
InputStream in = ExampleClass.class.getResourceAsStream(PUBLIC_KEY_PATH);
ObjectInputStream oin = new ObjectInputStream(new BufferedReader(in));
There we go 😉
You can get an
inputStreamfrom the file viaClass#getResourceAsStream(). For example:where you save
public.keyfile in the same directoryFooClass.javaFooClass.classis located in.