• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does new File("MyFile.txt") in a web-app generate the file in context root?

 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean my first impression was it should be generated in the current directory.

So if My Context name is MyApp(a J2EE project ..)

and it has this structure (in addition to others):

MyApp/src/main/java/com/utils (src/main/java is in the classpath)

And i have a file creation util class say MyFileCreator.java in the utils folder having

File f = new File("MyFile.txt");

the above code creates the file in MyApp and not in the utils package ... WHY?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Web apps do not have a concept of a "current directory", in particular not one tied to the currently executing class. It may be the root of the web app, but that's not specified, so you can't rely on it. That's why you need to use absolute paths in web apps.
 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm ..

Actually i realized that its eclipse that does that 'Create in context root' thing ..

You are right UD we cannot rely on that ..

Thanks bro
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For which ServletContext.getRealPath can help you out. For instance, to get a file in the root of the WEB-INF folder:
 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great

Thanks RP
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic