-1

I am able to read and alter a text file whilst in Eclipse, however when exported as a "Runnable JAR" it no longer works, it seems to me that the relative path is from inside the JAR, not to the JAR.

BufferedReader bufferedReader = new BufferedReader(new FileReader("RELATIVE PATH"));

I want it so that if I export my program as program.jar, and have file.txt in the same folder as program.jar, it opens file.txt if that is the relative path set.

Thanks for any help.

dbc
  • 104,963
  • 20
  • 228
  • 340
Matthew Winfield
  • 827
  • 4
  • 10
  • 25

1 Answers1

3

You can fetch the directory of your .jar file according to the solution of this answer and make your file accesses relative to that directory.

Beware of the warning concerning loading your class from a non-file location!

Community
  • 1
  • 1
alexgolec
  • 26,898
  • 33
  • 107
  • 159
  • This ends up loading from the location "workspace\projectName\target\classes" (in Eclipse, not tested in a .jar), I want it to load from "workspace" (The folder the file is located in). – Matthew Winfield Sep 26 '15 at 09:52