1

I am trying to move my spring application to spring boot

In my earlier spring application, I had two modules core and war. In core, the module was added as a dependency in war. In the core module, I read folders(iterate over the nested folders and load files) specified in resources. This used to work when I used to run it as tomcat.

Now I have migrated my application to spring boot. Reading of folder works in IDE but when I try to run it as

java -jar dnow/war/target/war.jar it gives me an error

java.io.FileNotFoundException: class path resource [datasources] cannot be resolved to absolute file path because it does not reside in the file system: 
       jar:file:/Users/kkadam/Work/repos/Project/dnow/war/target/war.jar!/BOOT-INF/classes!/datasources

My Code is given below

public static File[] getListOfFiles(String folder) {
        File file;
        File[] listFiles = null;
        String property = Utils.getConfDirectoryPath();
        if (!Utils.isEmpty(property)) {
            try {
                if (!property.endsWith("/"))
                    property = property + "/";
                file = new File(property + folder);
                listFiles = file.listFiles();
            } catch (Exception e) {
                logger.info("Folder " + folder + "does not found. Reading from resources");
            }
        }
        if (listFiles != null)
            return listFiles;
        else {
        return listFilesFromClassPath(folder);
        }
    }

    public static File[] listFilesFromClassPath(String folder) {
        File file;
        URL url = FileUtils.class.getClassLoader().getResource(folder);
        file = new File(url.getPath());
        return file.listFiles();
    }
Komal Kadam
  • 808
  • 1
  • 7
  • 18
  • Are you sure your jar file in correct directory? – Civan Korkmaz Jun 14 '21 at 12:18
  • 1
    You cannot read a `File` from a jar, technically it isn't a file in the sense of `java.util.File`. The `java.util.File` represents an actual physical file on the file system. When it is embedded in a jar/war this isn't the case. It works in your IDE (and Tomcat) because in your IDE it isn't packaged up as a jar but just reads plainly from the classpath (tomcat unpacks the war for reading so it is a physical file as well). So in short with a fat jar this will never work. – M. Deinum Jun 14 '21 at 12:19
  • https://stackoverflow.com/questions/10144210/java-jar-file-use-resource-errors-uri-is-not-hierarchical this might be helpful – SAQ Jun 14 '21 at 12:21

0 Answers0