1

I created a spring boot application which reads an excel file for data to be displayed in the front end so basically its the source of persistent data. I am able to run to correctly from my eclipse but when i create a spring boot jar and run it from the command line it fails as the files are not included in the jar.

I have tried two location src/main/resources and /src/main/webapp/WEB-INF/external/ but in both cases the files are not included.

Code :

private static final String FILE1 = "\\src\\main\\webapp\\WEB-INF\\external\\file1.csv";
private static final String FILE2 = "\\src\\main\\webapp\\WEB-INF\\external\\file2.csv";
private static String currentDirectory = Paths.get(".").toAbsolutePath().toString();
private static String completeAbsolutePath = currentDirectory
                      .substring(0, currentDir.length() - 1)
                      .replace("\\", "\\\\");

reader = new CSVReader(new FileReader(completePath + FILE1))

Error :

java.io.FileNotFoundException: c:\delete\src\main\webapp\WEB-INF\external\File1.csv

Can someone one please help?

Kenster
  • 23,465
  • 21
  • 80
  • 106
Joe
  • 296
  • 1
  • 7
  • 19
  • Please paste the code where you refer to the file – zuckermanori Sep 24 '17 at 12:15
  • https://stackoverflow.com/questions/22886083/how-do-i-run-a-spring-boot-executable-jar-in-a-production-environment – Mehraj Malik Sep 24 '17 at 12:16
  • Hi @zuckermanori Here is the code : I reading the data from two files (consider them as two separate database tables) private static final String FILE1 = "\\src\\main\\webapp\\WEB-INF\\external\\file1.csv"; private static final String FILE2 = "\\src\\main\\webapp\\WEB-INF\\external\\file2.csv"; private static String completeAbsolutePath = currentDir.substring(0, currentDir.length() - 1).replace("\\", "\\\\"); reader = new CSVReader(new FileReader(completePath + FILE1)) – Joe Sep 24 '17 at 12:21
  • And this is the error I m getting now : java.io.FileNotFoundException: c:\delete\src\main\webapp\WEB-INF\external\File1.csv (The system cannot find the path specified) – Joe Sep 24 '17 at 12:23
  • Please edit your question and add the code and error message – zuckermanori Sep 24 '17 at 12:24
  • @zuckermanori I just did. Please let me know if you require any more information. – Joe Sep 24 '17 at 12:29

1 Answers1

1

You have two problems here:

  1. You put the resources under src folder instead of creating a designated resources folder and put it there. To do that you should create a folder called "resources" at the same level as "src" right click the folder, select "Mark Directory As -> Resources Root"
  2. You are referring to the file with a relative path. This may work in eclipse but when you run it as a jar that paths are changed and relative path is not correct anymore.

    To use such a resource you should use the ClassLoader API ClassLoader.getResourceAsStream(String name) to get your resource.

    It should look similar to the following:

    reader = new CSVReader(new InputstreamReader(ClassLoader.getResourceAsStream(completePath +
    

    FILE1)));

zuckermanori
  • 1,675
  • 5
  • 22
  • 31
  • did you open the jar and saw the file aren't there? – zuckermanori Sep 24 '17 at 12:34
  • i was able to include the files in the jar using the statement provided by you . I have put the file in the resource folder and it was instead and they were included in the JAR file. thank you. The application is still not runnign from the command prompt but thats a different issue i believe. – Joe Sep 24 '17 at 21:15