• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

how to create a file in tomcat?  RSS feed

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all.i am trying to create a file in my web application.the file creater code works fine in a normal java project(project that contains main method).but when i tried to make it in the web application no file is created.does tomcat not allow creating files?
Best Regards.
 
Saloon Keeper
Posts: 5477
143
Android Firefox Browser Mac OS X Safari Tomcat Server VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using an absolute path? Web apps have no concept of a default directory, so relative paths don't work.

Also, the user account Tomcat runs under must have permissions to create files in that directory.

Lastly, make sure that there's no security manager that prevents files from being created.
 
ihsan kocak
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your repliy.yes i use relative path.i will use absolute path but i have no idea how to find out if there is a security manager prevents creating file.i use tomcat 7.
 
ihsan kocak
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it accepted the absolute path.thank you very much.
 
Sheriff
Posts: 21743
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to access / create a file inside the web application's folder itself, you should use ServletContext's getRealPath method. That way your application will still work if it's deployed in an environment where the web application is stored in a different location.
 
Bartender
Posts: 20728
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:If you want to access / create a file inside the web application's folder itself, you should use ServletContext's getRealPath method. That way your application will still work if it's deployed in an environment where the web application is stored in a different location.



Absolutely Positively DON'T create files inside the web application's folder.

I guarantee you'll regret it. Generally about the time that a new version of the webapp is installed and those files get deleted.

And getRealPath will return null if the WAR wasn't unpacked by the container and there therefore is no real filesystem path to the indicated resource. Unpacking (exploding) WARs is a feature that is optional in Tomcat, and although it's turned on by default, it can be turned off.

You can use getRealPath to read resources, although generally the getResource method is a better option. But I'm not kidding about writing into a WAR. I've been hit by this and hit hard. Always write your files to someplace outside of the WAR and the appserver.
 
Ranch Hand
Posts: 101
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been experiencing serious issues with this file thing too.
How about obtaining the working or home directory of the user under which Tomcat is currently running and then creating the file relative to that directory. Something like:


Does this kind of thing work as expected?
 
Sheriff
Posts: 67257
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Oladeji Oluwasayo wrote:Does this kind of thing work as expected?


What happened when you tried it?

And do you even know which user will be chosen?
 
Tim Holloway
Bartender
Posts: 20728
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Oladeji Oluwasayo wrote:Does this kind of thing work as expected?


What happened when you tried it?

And do you even know which user will be chosen?



Ah yes, there lies the rub.

A lot of people run tomcat as a root user (not recommended, for security reasons). And in Linux, the root home directory is /root, which isn't where I'd choose to have Tomcat store my files.

Those of us who are more paranoid run Tomcat under a server account such as "tomcat" or "apache". But these accounts may not have home directories, or their home directories might be read-only, or at a minimum, they may contain stuff that shouldn't be meddled with and/or that will get nuked when the server software is updated. I already made my opinion subtly clear on that issue earlier, as far as updating the webapps themselves goes.

The traditional place in Linux to put working files for an app is under /var/lib/xxxx, where "xxxx" is a directory that you create for the app itself (so xxxx is typically the appname). Other places sometimes used include the /opt/vendorname/xxxx directory or /srv/something-or-other.

In Windows there really aren't any conventions. Well, yes, there's "My Documents", I suppose. More recent versions of Windows have been converging towards a Unix/Linux type of directory structure, however.
 
Oladeji Oluwasayo
Ranch Hand
Posts: 101
Java Netbeans IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tim thanks.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!