I am trying to load files from my resource folder using the following code:
URL res = getClass().getClassLoader().getResource("test/test.txt");
File file = new File(res.toURI());
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
System.out.println(br.readLine());
br.close();
This works fine in Eclipse, but when I export it to an executable .jar file and run it (using java -jar), I get the following error:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(Unknown Source)
at Run.run(Run.java:34)
at Run.main(Run.java:7)
... 5 more
I don't understand why this is happening, since when I open my .jar file with WinRar, the files I am trying to read are in fact there.