Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="http://docs.oracle.com/javase/8/docs/api/java/io/File.html" rel="noreferrer"><code>java.io.File</code></a> and consorts acts on the local disk file system. The root cause of your problem is that <em>relative</em> paths in <code>java.io</code> are dependent on the current working directory. I.e. the directory from which the JVM (in your case: the webserver's one) is started. This may for example be <code>C:\Tomcat\bin</code> or something entirely different, but thus <em>not</em> <code>C:\Tomcat\webapps\contextname</code> or whatever you'd expect it to be. In a normal Eclipse project, that would be <code>C:\Eclipse\workspace\projectname</code>. You can learn about the current working directory the following way:</p> <pre><code>System.out.println(new File(".").getAbsolutePath()); </code></pre> <p>However, the working directory is in no way programmatically controllable. You should really prefer using <em>absolute</em> paths in the <code>File</code> API instead of relative paths. E.g. <code>C:\full\path\to\file.ext</code>. </p> <p>You don't want to hardcode or guess the absolute path in Java (web)applications. That's only portability trouble (i.e. it runs in system X, but not in system Y). The normal practice is to place those kind of resources in the <strong>classpath</strong>, or to add its full path to the classpath (in an IDE like Eclipse that's the <code>src</code> folder and the "build path" respectively). This way you can grab them with help of the <code>ClassLoader</code> by <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getResource-java.lang.String-" rel="noreferrer"><code>ClassLoader#getResource()</code></a> or <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getResourceAsStream-java.lang.String-" rel="noreferrer"><code>ClassLoader#getResourceAsStream()</code></a>. It is able to locate files relative to the "root" of the classpath, as you by coincidence figured out. In webapplications (or any other application which uses multiple classloaders) it's recommend to use the <code>ClassLoader</code> as returned by <code>Thread.currentThread().getContextClassLoader()</code> for this so you can look "outside" the webapp context as well.</p> <p>Another alternative in webapps is the <a href="http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContext.html#getResource-java.lang.String-" rel="noreferrer"><code>ServletContext#getResource()</code></a> and its counterpart <a href="http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContext.html#getResourceAsStream-java.lang.String-" rel="noreferrer"><code>ServletContext#getResourceAsStream()</code></a>. It is able to access files located in the public <code>web</code> folder of the webapp project, including the <code>/WEB-INF</code> folder. The <code>ServletContext</code> is available in servlets by the inherited <a href="http://docs.oracle.com/javaee/7/api/javax/servlet/GenericServlet.html#getServletContext--" rel="noreferrer"><code>getServletContext()</code></a> method, you can call it as-is. </p> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/2161054/where-to-place-and-how-to-read-properties-files-in-a-jsp-servlet-web-application">Where to place and how to read configuration resource files in servlet based application?</a></li> <li><a href="https://stackoverflow.com/questions/12160639/what-does-servletcontext-getrealpath-mean-and-when-should-i-use-it">What does servletcontext.getRealPath(&quot;/&quot;) mean and when should I use it</a></li> <li><a href="https://stackoverflow.com/questions/18664579/recommended-way-to-save-uploaded-files-in-a-servlet-application">Recommended way to save uploaded files in a servlet application</a></li> <li><a href="https://stackoverflow.com/questions/31255366/java-io-filenotfoundexception-when-trying-to-save-generated-file-in-web-applicat">How to save generated file temporarily in servlet based web application</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload