String path = ".";
String files="";
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
System.out.println("files::::::::::::"+files);
}
}
Say the above java file I saved under the path D:/spike/FileList.java
.
In the above code if I run in windows platform
, it will list the files under the 'spike
' directory.
But when I keep this in Linux environment
say the path is usr/local/apache/webapps/webtest/src/FileList.java
The result I get is files under root directory.
What I require is under the project root folder, i.e. in above case under the webtest directory.
How can I do the same. My requirement is I need to first list the files and then from the list of files I need to read sample.properties
file.
I know we can hard code the path to get the same. But without hard coding how can I get the list of files under the project root folder of webapps i.e, under webtest folder in my case.
I also tried by reading the environmental variables. But the problem here is my apache folder owner is spike and not root. So when I execute System.getenv()
what I get is only whatever variables the user spike has set.
But when I execute System.getenv()
from any other folder whose owner is root, then I get the complete environmental variables.
So is there any way I can get the project root folder by using the above java code snippet without hard coding the path?
By the way this is a web application deployed in tomcat. First the app will read the details from the server.properties file. But Im not supposed to hard code the path as the path changes from system to system. So my intention is that the code read the properties file from the project starting folder.