-1

I have this peace of code to load a text file inside of a servlet:

String lFileName = mServletContext.getRealPath(mFile);
InputStream lInputStream = mServletContext.getResourceAsStream(lFileName);
InputStream lInputStream2 = mServletContext.getResourceAsStream(mFile);

Both InputStream's are null. I have absolutly no idear why.

The value of mFile is "file.txt". The value of lFile is "C:\development\workspace\MyGwtApp\war\file.txt".

if I navigate with my explorer to that directory the file file.txt is in it...!

I test my gwt application with the super dev mode. Compile the gwt app runs without problems.

Do you see the problem?

nano_nano
  • 12,351
  • 8
  • 55
  • 83

1 Answers1

-1

getResourceAsStream definition

Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader.

This means that you can read mFile if it exists in your classpath like under WEB-INF/classes. So place your file in your src directory where your java classes exists and look if the file comes to the classes directory and just use its name to get it as resource. Example: filename = "file.txt"

sgpalit
  • 2,676
  • 2
  • 16
  • 22
  • 2
    We have got a downvoter thats a good sign, someone gives response :) – sgpalit Mar 31 '16 at 13:57
  • 1
    nearly correct. It is not necesarry to put the file in the classes directory. But in my case it was necessary to put it in the WEB-INF directory. I put it in the war directory. Nevertheless you give me a good hint. – nano_nano Apr 01 '16 at 07:47