• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Restful service read text file

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a Jersey Restful service to be deployed on Tomcat via a war file.

The service needs to read data is 3 text files. The text files need to exist on the file system or read from the classpath. I have tried to read the data from the file system and classpath but neither are working. I would be happy with either way - it doesn't matter.

If it was to use the following code,can someone tell me where to place the text file specified so that the code picks up the file?


I am getting a null pointer exception.

If I was to read the file from the file system, use the following code, where do I place the files in my Jar?

I am getting a FileNotFound exception.

Any suggestions are welcome.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Stating the file name like that causes the JVM to look for that file in the "current" directory - you have no control over that directory so the file is not found.

Use the absolute file location or one of the application relative methods for addressing files.

Bill
 
David McWilliams
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill but that is what I'm asking really. I have been using every combination of paths and placing the file in different locations but my app cannot find the file. Can you give me an example of a path string and location?
 
David McWilliams
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone any ideas?
 
Greenhorn
Posts: 14
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are your text files located in your project or better said in the war? Or are you placing them on the file system outside of the war? They need to be located in the classpath so in the WEB-INF/classes or for instance in a maven project in the src/main/resources directory.

Failing this, you can try this.getClass().getClassLoader().getResourceAsStream("/myfile.txt") with the '/' in front of the file name. This, I believe, looks in the directory where "this" is located which is the location of the class doing the lookup.

 
David McWilliams
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Enochson wrote:Where are your text files located in your project or better said in the war? Or are you placing them on the file system outside of the war? They need to be located in the classpath so in the WEB-INF/classes or for instance in a maven project in the src/main/resources directory.

Failing this, you can try this.getClass().getClassLoader().getResourceAsStream("/myfile.txt") with the '/' in front of the file name. This, I believe, looks in the directory where "this" is located which is the location of the class doing the lookup.



I copied my text file into the WEB-INF/classes folder and used this.getClass().getClassLoader().getResourceAsStream("/myfile.txt") and it worked.

Thanks all.
 
reply
    Bookmark Topic Watch Topic
  • New Topic