title: Spring ClassPathResource - cannot be opened because it does not exist
body: <p><strong>UPDATE:</strong> I'll still keep Artem Bilan's answer marked as correct, but I still felt I needed to point this out for any future readers. It seems I was misunderstanding the concept of the 'default value' for the <code>@Value</code> annotation. The functionality I was hoping to achieve by using</p>
  
  <pre><code>@Value("${someProp:defaultFilePath}")
  private Resource resourcefilePath;
  </code></pre>
  
  <p>was that, should the file path someProp defined in application.properties throw an exception (i.e. file not found), it would then attempt to use the defaultFilePath (i.e. the one above). What defining a default value actually does is that, if the property itself (someProp) does not exist (not defined or commented out) in the application.properties file, it then attempts to use the default value instead.</p>
  
  <hr>
  
  <p>I'm using Spring Integration Sftp for SSH file transfer. Using Spring Boot so no xml files. Before configuring the <code>DefaultSftpSessionFactory</code> object, I define a resource which contains a .txt file that has the private key required for the sFtp authentication.</p>
  
  <p>Previously, I used <code>FileSystemResource</code> like this:</p>
  
  <pre><code>Resource resource = new FileSystemResource("C:/absolute/path/to/my/private/key/privateKey.txt");
  </code></pre>
  
  <p>This worked just fine. However, this application will eventually be put in a cloud environment, which means absolute paths like that won't work anymore. I'm trying to instead use ClassPathResource but it's not working no matter what I try. So far I've tried the following:</p>
  
  <pre><code>Resource resource = new ClassPathResource("privateKey.txt");
  Resource resource = new ClassPathResource("privateKey.txt", SomeClassInClassPath.class);
  Resource resource = new ClassPathResource("com/my/package/name/privateKey.txt");
  </code></pre>
  
  <p>My directory structure looks something like this:</p>
  
  <pre><code>ProjectFolder -&gt; src -&gt; main -&gt; java -&gt; com -&gt; my -&gt; package -&gt; name -&gt; various java classes
                                                                          privateKey.txt
                               -&gt; resources -&gt; etc...
  </code></pre>
  
  <p>There's more but this is a simplified version of it. Can anyone help figure out how I can get it to recognize the path to my .txt? I keep getting <code>java.io.FileNotFoundException: class path resource [resource] cannot be opened because it does not exist</code> no matter what I try.</p>
  
  <p><strong>EDIT:</strong> WAR structure:</p>
  
  <pre><code>ProjectFolder -&gt; META-INF -&gt; maven -&gt; etc..
                -&gt; org -&gt; springframework -&gt; boot -&gt; etc..
                -&gt; WEB-INF -&gt; classes -&gt; com
                                      -&gt; public
                                      -&gt; application.properties
                                      -&gt; privateKey.txt
  </code></pre>