0

I'm trying to save a YAML formatted file packaged in the JAR to the user's disk.

The file is named config.yml and sits directly under one of the project's source folders. I can confirm that the file is packaged in the JAR with WinRAR.

I am using the following code to get the file from the JAR:

File file = new File(this.getClassLoader().getResource("config.yml").getPath());

And I am using this code to copy the file to a directory:

FileUtils.copyFileToDirectory(file, this.getDataFolder());

The getDataFolder() method is implemented by a reliable 3rd party API. However, when I use a file instance defined with the same getDataFolder() File instance and the path "config.yml":

new File(this.getDataFolder(), "config.yml")

The console logs a FileNotFoundException. The file path given in the stack trace is this: "file:\C:\Users\Evan\Desk top\Test%20Server\plugins\lottocrates-1.0.jar!\config.yml" which seems correct. I tried opening the file with run prompt, which I was able to open the JAR with, but not the config.yml file.

Evan McCoy
  • 11
  • 3
  • `file:\C:\Users\Evan\Desk top\Test%20Server\plugins\lottocrates-1.0.jar!\config.yml` is trying to reference the `config.yml` from inside the `lottocrates-1.0.jar` as a `File`, which is obviously wrong. This `File file = new File(this.getClassLoader().getResource("config.yml").getPath());` is your first problem. Use `Class#getResourceAsStream` to get a `InputStream` to the resource, copy the contents out to a `File` and then load the file, and no, this is not what you're doing. – MadProgrammer Jun 19 '15 at 21:55
  • Thanks! Worked perfectly. I ended up using CommonsIO to make it fairly painless. (FileUtils#copyInputStreamToFile) – Evan McCoy Jun 19 '15 at 22:05

0 Answers0