0

I have converted my JavaFX project into a jar file and these posts helped me to successfully read media files from my local environment that are needed in my project:

Now at one part of the project I need to create a folder in my pc during the project runtime, that will store some data gained through the program. Now I am using this command to execute that purpose:

String file_name= String.valueOf(Main.class.getResource("data stored/"+name+"-"+Long.toString(time)));
    new File((file_name)).mkdirs();

Here I just followed the similar steps that I used for reading the file ( Which is mentioned in the attached posts). But when I run the jar file, everything works fine except that, the folder is not created. And if I don't write this part: Main.class.getResource, the jar file shows an error which is obvious. I think the reason might be, for reading, it needs the reference of the main class, but for creating a folder, referring with respect to a class is not correct.

My path of main class: src/brainwave/Main.java

Path of the folder I want to create: src/data stored

Any suggestion please how can I solve this issue? Thanks.

JoSSte
  • 2,953
  • 6
  • 34
  • 54
asifhaider
  • 13
  • 5
  • please keep your tags focused - this is plain java, unrelated to javafx. And stick to java naming conventions when showing code publicly – kleopatra Feb 22 '21 at 11:10
  • you cannot modify the jar file while the application is running. If you want to do that, you will have to do some creative copying of the jar, adding a folder, and repackaging, and then run the new version... – JoSSte Feb 22 '21 at 11:13
  • a `.jar ` is only a way of packaging your java aplpication for distribution - essentially it's a `.zip` file with some information about the java application stored inside... – JoSSte Feb 22 '21 at 11:15
  • How do you get the path where the file should be saved? – Renis1235 Feb 22 '21 at 12:02

1 Answers1

0

getResource will retrieve a path into the JAR file, in case the class file is also coming from there; otherwise a normal path, if not running from JAR.

Use a plain File to get a path to a file/directory (e.g. new File("data/files/")) that is not saved in the JAR