Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

deployment

 
Simon Ingram
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have made a small modification to the Beer app from the HF Servlets & JSPs tutorial. My BeerExpert class reads a text file. The class works fine in the development environment when I test it, but when I deploy and run, I get a java.io.FileNotFoundException pretty much whereever I put the txt file.





My BeerExpert class has the statement:



the BeerExpert class is in: BeerV1\source\com\example\model

I tried the file in BeerV1\classes, then in:
BeerV1classes\com\example\model

and finally in:
BeerV1classes\com\example\web

Any ideas, anyone?

regards
Simon
[ March 18, 2006: Message edited by: Simon Ingram ]
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your app name is BeerV1 and so your webapp folder is BeerV1, then put your beer.txt file in the webapp root directory, as pretty much everything about pathes in a webapp is relative to webapp root, even absolute pathes. You cannot think about your webapp classes as running in their classes/package dir, you must think your webapp as running in its application context (aka servlet context) which directory is the webapp root. You should get better results.
[ March 18, 2006: Message edited by: Frederic Esnault ]
 
Simon Ingram
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frederic, it was worth a try but produced the same result. The system cannot find the file and I have run out of places to put it!
 
Mihai Lihatchi
Ranch Hand
Posts: 138
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Well. One not very smart solution would be to enter the full path in the file constructor.
File file=new File("/home/mihai/webapps/beer1.txt");
Another would be to copy the file to the folder from which you read BEFORE starting the AppServer (Tomcat in your case )
-> something like /beer/WEB-INF/classes/beer1.txt -> depending on your class hierarchy here.
 
Chandra Sagi
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Files under WEB-INF or META-INF are secured from outside access directly. From inside I guess we need to read it as byte stream. There are some methods which I am not sure like getResourceAsStream(). Please look at it this way.

Thanks
Chandu
 
Bhavik Patel
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure but you can give it a try .. When You specify beer.txt it will try to find out the file in the server's shared directory(For tomcat in shared directory )
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry my head was away

My explanation about the path is correct but i didn't see you were using new File("...");

The way to do what you want here is to use one of the context methods :
getResource()
getResourceAsStream()

Both of them use a path (with a leading /) relative to the context root directory, as i said before. I prefer to use getResourceAsStream (which returns an InputStream), but you may use the other one which returns a URL.

I made a quick code sample to help you. My file toto.txt is in the context root directory.



This time, it definitely SHOULD work better, and sorry for my first post, where i focused on the path and didn't see the File usage
 
Simon Ingram
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was on completely the wrong track, so thanks guys! I will try the suggestions.
 
Simon Ingram
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

Thanks for your suggestions Frederic. I certainly missed the point about requiring a resource context method. Your example however, covers the case where my scriptlet is trying to process the beers.txt file but I am trying to use the beers file in my model, that is, in the helper class BeerExpert. Are you really saying that the model (a plain old java class) cannot access a file once deployed?
 
Simon Ingram
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
come on guys! I'm getting lonely here talking to myself! This is an interesting log. Anyone reading our beloved HF book knows the example, so the question is straight forward - Can the BeerExpert read a text file and if so where is it deployed? Any response will be received with abject delight.
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, sorry didn't see your previous post.

The answer is yes, I'm saying that. A Model class even if it's not extending HttpServlet or whaterver J2EEish thing, is still called FROM the servlet and (most important), from an application context.

The context is the key here. You are not in a normal J2SE app, but in a J2EE application, running in a Web container, in a web/j2ee server.

Access to system I/O directly goes against the webapp philosophy. And worse, it may work in one web container, but not in another, as each container may use different directories, and/or different who-knows-what.

Don't get me wrong, I don't say it WON'T work, maybe it's possible, I have no 100% sure answer there, but even if it was, you definitely shouldn't (because it's... hmmm... BAD, and because it may not work in another environement).

Hope this helps, and sorry for the delay.
 
Simon Ingram
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frederic, thanks for the reply. OK I understand what you mean, which means that we can't have helper classes that read files. We have to process the files into objects or beans and load them into attributes. Now that I write this it seems so obvious! ANyway, thanks for your help and congratulations on passing the exam!
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for you congrats

But remember, don't give up files for webapps, you MAY use files like properties files and so on. Just remember to use your Front Controller or whatever you use to get them as InputStream (I prefer IS) or as URL with the ServletContext method.

You cannot deal with everything using attributes. Some things must be declared declaratively. But you MUST make clear in your mind the servlet context (also called Application context) you're running...well, hmm, I'm starting again my "J2EE paradigm" speech...sorry But I think you got the point, which is good for you !

Happy I helped.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic