• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

how to create a file in tomcat?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7355
170
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it accepted the absolute path.thank you very much.
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 25656
183
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 67645
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 25656
183
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tim thanks.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic