0

Java project build by Gradle.

I want to create file in \build\resources\main\db\sqlite\insert-data.sql

I try this:

  protected File getDestinationFile(String baseDir) {
    try {
        URL testURL = ClassLoader.getSystemClassLoader().getResource("db/sqlite");
        String pathName = testURL + "/" + DEFAULT_FILE_NAME_INSERT_DATA;
        logger.debug("pathName = " + pathName);
        File newFile = new File(pathName);
        newFile.createNewFile();
        return newFile;
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        return null;
    }
}

But I get error on runtime:

[17.06.2022 00:19:38.223] MyClass.getDestinationFile(SQLiteStrategy.java:170) ERROR:
   java.io.IOException: 
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:1012)
Alexei
  • 14,350
  • 37
  • 121
  • 240
  • What does `testURL` look like? Maybe it doesn't point to where you expect it. – akarnokd Jun 16 '22 at 21:58
  • A resource is not a file, it is data packaged in a JAR. – Mark Rotteveel Jun 17 '22 at 11:41
  • Alternatively, look at [How to include SQLite database in executable Jar?](https://stackoverflow.com/questions/12019974/how-to-include-sqlite-database-in-executable-jar) if you want to open a read-only sqlite database. – Mark Rotteveel Jun 17 '22 at 11:46
  • @akarnokd pathName = file:/C:/dev/temp/m2cm/app/build/resources/main/db/sqlite/insert-data.sql – Alexei Jun 17 '22 at 17:20
  • 1
    That's not a valid file name. There is URL to URI to File chain to get a valid File object https://stackoverflow.com/a/54123394/61158 – akarnokd Jun 17 '22 at 17:41

0 Answers0