This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileOutputStream(), FileReader() do NOT accept relative path in servlet ?

 
Frank Sikuluzu
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without using any servlet, I found that
new FileOutputStream("data/output");
new FileReader("data/input");
work well.
However, when do the same thing from servlet, they do NOT work unless I give a full path like new FileOutputStream("C:\\mydir\\data\\output");
Is it true and how do we get it around ?
thanks.
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about running this piece of code to find out where are you trying to create your files?
 
Billybob Marshall
Ranch Hand
Posts: 202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Sikuluzu:
Without using any servlet, I found that
new FileOutputStream("data/output");
new FileReader("data/input");
work well.
However, when do the same thing from servlet, they do NOT work unless I give a full path like new FileOutputStream("C:\\mydir\\data\\output");
Is it true and how do we get it around ?
thanks.

You shouldn't be using relative paths in the first place. That makes your apps fragile, as it requires them to be started from a particular directory. If you want to read files, read them from the classpath:
InputStream is = getClass().getResourceAsStream("/yourfile");
If you want to write files, you can first read a config file which tells you the location the user wants them written - then write them there.
 
Frank Sikuluzu
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks lot for the response. Just want to know -- besides how to solve the problem in alternative way, why does it behave like that ? Is it supposed to be like that or is it because I miss something ?
 
Billybob Marshall
Ranch Hand
Posts: 202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Sikuluzu:
thanks lot for the response. Just want to know -- besides how to solve the problem in alternative way, why does it behave like that ?

Because the web server is started from a directory that maybe you didn't expect. Maybe you are expecting relative directories to be relative to the calling class file? That is not the case. Relative directories are relative to the current working directory, usually where the java process was launched from.
 
Frank Sikuluzu
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Billybob Marshall:

Because the web server is started from a directory that maybe you didn't expect. Maybe you are expecting relative directories to be relative to the calling class file? That is not the case. Relative directories are relative to the current working directory, usually where the java process was launched from.

As a beginner, I would like to make sure I understand you clearly. My calling class "Test.class" is put at myApp/WEB-INF/classes/ folder and "data" folder is at is put at myApp/WEB-INF/classes/ too. So I assume it searches from myApp/WEB-INF/classes/ because Test.class is there. Are you saying the web container may search it from somewhere else ? BTW, I add "." in the classpath for the web container but hoping it can search from current directory of the calling class but it still fails. Sad. Please confirm if I understand you correctly.
 
Billybob Marshall
Ranch Hand
Posts: 202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Sikuluzu:

My calling class "Test.class" is put at myApp/WEB-INF/classes/ folder and "data" folder is at is put at myApp/WEB-INF/classes/ too. So I assume it searches from myApp/WEB-INF/classes/ because Test.class is there. Are you saying the web container may search it from somewhere else ?

Regardless of whether it's the web/app server, or just a normal app, they are all running the 'java' process from some directory. It's nothing particular about it running as a servlet. As I already said, I'll say it again... Relative directories have nothing to do with the location of .class files - only from where you launched 'java' from. In your prior experience, I would guess you happened to launch 'java' from the same directory as your .class file(s) lived in. Never count on that.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic