Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Restful service read text file

 
David McWilliams
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Anyone any ideas?
 
Brian Enochson
Greenhorn
Posts: 14
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!