0

I wrote a little Java app for analyzing .csv files. Now I want to keep reading from and writing to a .txt file, which acts similar to a mini-database. For this purpose I simply added the .txt in my project and used the Files.readString(Path) and Files.write(Path path, byte[] bytes) methods.

When I do this in IntelliJ I have no problems but as soon as I build/export the file with Maven and started with the created launcher the app didn't work because the project structure / file organization isn't the same anymore.

I also tried to just add the .txt file to the exported folder afterwards but even then I couldn't manage to implement a relative path to the file.

I'm still relatively new to programming and it's just a small app so I don't think mySQL would fit my needs. I've also read that I could store the data in a property file but I don't know if that would be the right way to archive what I want. Certainly it must be possible to somehow keep a .txt for reading and writing in your exported project. Does someone have an idea?

2 Answers2

0

If you use a ´*.txt` file for storing, that file cannot be inside the jar because the jar cannot change its contents while running.

You need to put the file somewhere else, either at some dedicated location (let the user choose/configure one), or next to the jar. To figure out your own execution path, you can use

How to get the path of a running JAR file?

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • Thank you for the answer! I managed to fix it. I just put a folder in the same directory as the .jar file and then started the Paths from the directory containing the .jar. So I only had to use Path.of("resources/database.txt"). Turns out my problem was actually to put everything in a single, executable .jar file. I still have to start it with this command inside a .bat file though: java --module-path javafx-sdk-11.0.2\lib --add-modules=javafx.controls,javafx.fxml -jar csvTool1.2.jar Do you know how I could implement that into the .jar somehow? – arrabiataHunter Mar 23 '20 at 17:25
-1

Maven is one tricky tool. You need to go to the pom file and add the resource. Unable to add resources to final jar for maven project in Intellij.

-I hope this helps Trader