Where does eclipse search for a file if full path is not provided?

Viewed 35

In NetBeans (Java Application Project) if I put a file in the root directory of a project NetBeans can find the file. For example

FileReader reader = new FileReader(new File("test.txt"));

Here test.txt file is in the root directory of the project. This is what the root directory look like.
-> Project Folder
--> build
--> nbproject
--> src
--> test
--> build.xml
--> manifest.mf
--> test.txt

I have tried putting a file in the root directory of an eclipse dynamic web project with the same code. But it cannot find the file. This is what the root directory for eclipse looks like.
-> Project Folder
--> .settings
--> build
--> src
--> .classpath
--> .project
--> test.txt
In which location does eclipse search for files by default? Is it different for different types of projects?

1 Answers

The root of the project is not deployed to the server, plus you can not rely on the "working directory" being anything other than what the server chooses for your application. Place the text file under the src/main/java or src/main/webapp folder and read the file's contents using your Servlet class' #getResourceAsStream(String) or current Servlet context using ServletContext#getResourceAsStream(), respectively.

Related