0

I am trying to build a .war file with a with a custom .txt file added inside the project. which will be accesses at run time after the .war has been built and deployed.

For example:

My project in eclipse will look as such:

src
|  |-Package1
|      |-Config.class
|  | Package2
|       |-Controller.class
files
|  |-text.txt
|
|-Pom.XML

I am trying to read in the text.txt file in which I can use to print out text in the controller class. This must be achieved after the .war.

Currently so far I have the following:

<webResources>
        <resource>
            <!-- this is relative to the pom.xml directory -->
            <directory>files</directory>
        </resource>
</webResources>

Java Method:

@RequestMapping(value="/")
    public ModelAndView test(HttpServletResponse response) throws IOException{

        BufferedReader br = new BufferedReader(new FileReader("files/text.txt"));
        String curr;
        try {
            while((curr = br.readLine()) != null)           {
                System.out.println(curr);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new ModelAndView("home");
    }

Does anyone have an idea how this can be achieved? I must be able to get the abs path of it in the .war directory as I plan to use a cmdline to execute it.

E-Riz
  • 31,431
  • 9
  • 97
  • 134
DarrenW
  • 108
  • 1
  • 2
  • 13
  • 1
    What's your error ? also http://stackoverflow.com/questions/13242782/reading-a-text-file-in-war-archive You should not that you can't get the absolute path of the file.txt if it's in a war. – Asoub Sep 16 '16 at 13:51
  • Also note that the standard location for non-Java source files in a maven project is `/main/resources`. Putting them there, with a standard maven WAR build, will result in the files being on the application's runtime classpath. – E-Riz Sep 16 '16 at 13:57

0 Answers0