0

So I have cleaned and build it my project as a runnable jar file. There are some scenes that need to get data from files in the project. But for some reason, the file path's are not correct. My project in ide is like this

enter image description here

The path in my project is "src\ch\makery\address\util\settings.xml" but when I run my .jar file it becomes c:\users\bob\Desktop\src\ch\makery\address\util\settings.xml which will than throw a FileNotFoundException, is there a way to fix this?

C.Ikongo
  • 1,706
  • 1
  • 16
  • 15
  • Don't use the file api to access resources. Use the classloader/class to access the resource url/a stream created from the resource entry of the jar. You may also need to tell your ide to include the resource files in the jar. Note that writing is not possible in this case. If you need to write to the file, create a copy of the settings in a convenient place unless the file already exists. You could e.g. [store the settings in the home directory.](https://stackoverflow.com/a/3784695/2991525) – fabian May 13 '18 at 12:30
  • So I can't just save everything in my jar file, I would need to save it on the user's computer? And use that path to read/write when needed. – C.Ikongo May 13 '18 at 12:47
  • @fabian do you have any good example? – C.Ikongo May 13 '18 at 15:13

1 Answers1

0

If you read your resource files using ClassLoader.getSystemResourceAsStream() then you wont get FileNotFoundException after exporting project to runable jar

    InputStreamReader isr = new InputStreamReader(ClassLoader.getSystemResourceAsStream("ch\\makery\\address\\util\\settings.xml"));
    BufferedReader br = new BufferedReader(isr);
Umer Farooq
  • 762
  • 1
  • 8
  • 17
  • Don't use backslashes for the path. Use normal slashes. Furthermore it's probably better to use the classloader used to load the class iding the resource instead of the system classloader. E.g. `DateUtil.class.getResourceAsStream("/ch/makery/address/util/settings.xml")`. Moreover there are better classes for reading xml files than `BufferedReader`... – fabian May 13 '18 at 16:13
  • @fabian I am reading the xml using jaxb. I just need to get the file path. – C.Ikongo May 13 '18 at 17:35
  • @C.Ikongo Using forward slash also gives null pointer exception ? – Umer Farooq May 13 '18 at 18:27
  • @UmerFarooq yeah, I am not sure what I am doing wrong? I have spent some much hours trying to fix this bug. Is there a working java project or code someone can post? – C.Ikongo May 13 '18 at 18:29
  • @C.Ikongo Make sure you used `ch/makery/address/util/settings.xml` not `src/ch/makery/address/util/settings.xml` – Umer Farooq May 13 '18 at 18:36
  • @UmerFarooq it worked, I just had some typos and missed placed some files. How do I get the path? Or would I have to read and write them into a temp file and use that? – C.Ikongo May 13 '18 at 18:48
  • @C.Ikongo `ch/makery/address/util/settings.xml` is actually path to `settings.xml` file.. what do you mean by getting path here?? – Umer Farooq May 13 '18 at 19:03