0
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.

Susheel
  • 21
  • 6
  • The path where your java source file is stored is absolutely irrelevant for the output of the program that is coded therein. Just do a `cd` to the directory you want to list before you run your program. – Ingo Dec 10 '13 at 13:18

3 Answers3

2

This is not true, your code on Linux prints the files in the current folder. I just tried it.

As to tomcat, see here What determines the current working directory of Tomcat Java process? and/or look for similar information on the Tomcat site.

Community
  • 1
  • 1
  • Well I dont know how exactly you tired. Did u try from within tomcat? – Susheel Dec 10 '13 at 13:19
  • No, I put your code into a java file with main method, compiled it. Ran it. It works. – try-catch-finally Dec 10 '13 at 13:20
  • tomcat - very important information absent from your question! – NickJ Dec 10 '13 at 13:21
  • If you run it from Tomcat, you should check where the working directory of tomcat is (at runtime). It should print the files from that directory. But . is the current folder, you can rely on that. – try-catch-finally Dec 10 '13 at 13:21
  • Your current folder may be / root even if your code is physically not there. You have somewhat wrong understanding of this. – try-catch-finally Dec 10 '13 at 13:28
  • Sorry NickJ...I have added the info of tomcat now to the main question. – Susheel Dec 10 '13 at 13:33
  • Thanks try-catch-finally for the link. So in that case I cant use "." as the path, as my tomcat is run using script. So let me try by start the server from the tomcat installed directory. – Susheel Dec 10 '13 at 13:33
  • Thanks try-catch-finally. That way worked. Only thing is as Im starting my tomcat from $TOMCAT-DIR/bin/startup.sh my root folder in that case is $TOMCAT_DIR/bin. – Susheel Dec 10 '13 at 13:50
0

You can try with the current working directory:

String path = System.getProperty("user.dir");

But this depends where exactly is the CWD, e.g. how was this application started?


In the context of a web application, you can do (as long as you have the ServletContext object):

ServletContext application = ...
String path = application.getRealPath("/");

This will give you the root of the web application, you can navigate to the required directory from there.

Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
  • I had tried the above code. In my case it is displaying "/". That means its showing root. But my code is not in root folder. Its under tomcat, the path which I mentioned above. Again the owner of this whole directory is spike. I dont know how that can effect it – Susheel Dec 10 '13 at 13:20
  • By the way this is a web application. 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. – Susheel Dec 10 '13 at 13:28
  • Thanks Nikos for the reply. I will try that way too. Only thing here is, if I need to get the Servlet Context I need extend my java class with HttpServlet. – Susheel Dec 10 '13 at 13:47
0

If your file sample.properties is located in the root folder, and actually is a properties-file I would try this loading the file and printing the content:

    String filename = "sample.properties";
    Properties properties = new Properties();
    properties.load(getClass().getClassLoader().getResource(filename).openStream());
    properties.list(System.out);

getClass.getClassLoader() will get the location of the rootfolder. If you skip getClassLoader you will get the folder of the package in your class.

(If you are running this in a standalone java app the properties file needs to be located in the classes folder.)

Kennet
  • 5,736
  • 2
  • 25
  • 24
  • Thanks kennet for the suggestion. – Susheel Dec 10 '13 at 13:48
  • Kennet your method will work when in stand-alone java code. But in my case its inside tomcat directory. In that case this will throw exception. – Susheel Dec 10 '13 at 13:59
  • My guess is that the exception is from FileNoutFound/IOException, if your code is in a servlet, try `ServletContext context = this.getServletContext(); URL resource = context.getResource(filename); properties.load(resource.openStream());` – Kennet Dec 10 '13 at 14:58