1

My GWT project runs nicely in developement mode but when I put it on server it can't find an XML file.

My file is in src/main/webapp and when I do mvn install it shows up in target/<projectname>-1.0-SNAPSHOT

I try to access the file like this:

FileInputStream inputStream = new FileInputStream("testobj.xml");

and it throws

java.io.FileNotFoundException: testobj.xml (The system cannot find the file specified)

really puzzled by it .. haven't found any useful links on this either.

Durandal
  • 5,575
  • 5
  • 35
  • 49
Mihkel L.
  • 1,543
  • 1
  • 27
  • 42

3 Answers3

2
FileInputStream inputStream = new FileInputStream("testobj.xml");

To make that a valid path you have to place the xml in same folder where your class file is there.

And the good practice is that put the file in WEB-INF folder and access the path like

getSevletContext.getRealPath("/WEB-INF/resources/testobj.xml");

You might placed the file in src and it is taking from system path. Once you compile the project, your java files converts to class files and places in WEB-INF/classes folder, where the context has been changed.

To maintain the consistency for both in development mode and live environment access files from WEB-INF folder with real path.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

It can be seen in development because testobj.xml is able to be found on the path in development. After your project is packaged and built it needs to be in your WEB-INF folder in the war. It is generally good practices to put your resources in src/main/resources as well, not the root folder.

Whatever you are using for your build will need to copy your resources to WEB-INF when creating a war. If you are using maven see this thread for how to accomplish this: Maven: how to get a war package with resources copied in WEB-INF?

Community
  • 1
  • 1
Durandal
  • 5,575
  • 5
  • 35
  • 49
1
FileInputStream inputStream = new FileInputStream("testobj.xml");

This line tries to access "testobj.xml" in the process's current directory. When you run this within a web app, it'll look for the file within the application server process's current directory. This directory could be anything, and it's unlikely that the file will be there.

The normal way to read resources packaged with the web app is to use the web app's ClassLoader:

InputStream is = getClass().getClassLoader().getResourceAsStream("resources/testobj.xml");

This will automatically search the web application's deployment files for the named file. See this question for more discussion.

Community
  • 1
  • 1
Kenster
  • 23,465
  • 21
  • 80
  • 106