Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6109257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:23:04+00:00 2026-05-23T14:23:04+00:00

I write a Java servlet that I want to install on many instances of

  • 0

I write a Java servlet that I want to install on many instances of Tomcat on different servers. The servlet uses some static files that are packed with the war file under WEB-INF. This is the directory structure in a typical installation:

- tomcat
-- webapps
--- myapp
---- index.html
---- WEB-INF
----- web.xml
----- classes
------ src
------- .....
----- MY_STATIC_FOLDER
------ file1
------ file2
------ file3

How can I know the absolute path of MY_STATIC_FOLDER, so that I can read the static files?

I cannot rely on the “current folder” (what I get in a new File(“.”)) because it depends on where the Tomcat server was started from, which is different in every installation!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:23:05+00:00Added an answer on May 23, 2026 at 2:23 pm

    You could use ServletContext#getRealPath() to convert a relative web content path to an absolute disk file system path.

    String relativeWebPath = "/WEB-INF/static/file1.ext";
    String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
    File file = new File(absoluteDiskPath);
    InputStream input = new FileInputStream(file);
    // ...
    

    However, if your sole intent is to get an InputStream out of it, better use ServletContext#getResourceAsStream() instead because getRealPath() may return null whenever the WAR is not expanded into local disk file system but instead into memory and/or a virtual disk:

    String relativeWebPath = "/WEB-INF/static/file1.ext";
    InputStream input = getServletContext().getResourceAsStream(relativeWebPath);
    // ...
    

    This is much more robust than the java.io.File approach. Moreover, using getRealPath() is considered bad practice.

    See also:

    • getResourceAsStream() vs FileInputStream
    • What does servletcontext.getRealPath("/") mean and when should I use it
    • Where to place and how to read configuration resource files in servlet based application?
    • Recommended way to save uploaded files in a servlet application
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a java program that acts as a user interface to
I'm using Java Logging API in a simple Servlet application that uses Maven and
Hey I'm new to java servlets and I am trying to write one that
I'm writing HTML code for a Java servlet. I first write the code in
I wrote some code to read a file in my Java Servlet class. (I'm
I need to write a Java Comparator class that compares Strings, however with one
I wrote a little Java servlet that would dynamically generate an image button given
I am writing a Java servlet, using Tomcat as the container, which creates and
I'd like to write a Java program that takes arbitrary data and stores it
I've got a Java servlet that handles file uploads, but resides in the intranet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.