views:

36

answers:

1

Hello!
I have a webapp running in my local tomcat. The path is: tomcat/webapps/myproject.
I have some resources in this project all in a folder with this path: tomcat/webapps/myproject/resources

So, I am trying to acces to this resources from a java project, using a file config.properties. In this file I have something like this:

tomcat.url= http://localhost
tomcat.port=8080
tomcat.resources=/myproject/resources

I tryed also different combinations of / or \ but I get this error runing my project:

Trying to acces to a directory that does not exist

My java code:

Configuration config = new PropertiesConfiguration("config.properties");    
String sourcePath = config.getString("tomcat.resources");
//And I try to list this folder  
File dir = new File(sourcePath);
String[] children = dir.list();         
if (children == null) {          
            // Either dir does not exist or is not a directory
            throw new ServiceExecutionException("Trying to generate Metadata in a directory that does not exist");                  
            } 

I don't know what is wrong, in projects I made before, simliar to this, I had something similar and it found everything.
Any ideas?? Thanks in advance

+1  A: 

the file must be located at tomcat/webapps/myproject/src/main/resources Check update.

I don't know how are you accessing the file, but just in case I'd recommend you to use PropertiesConfiguartion

 private static final String CONFIGURATION_PATH = "config.properties";
 PropertiesConfiguration configuration = new PropertiesConfiguration(CONFIGURATION_PATH);

Edited

The problem is that you are trying to open a file located (keeping in mind that "/" is your root file system) at /myproject/resources when your file is located at (probably) /var/lib/tomcat6/webapps/myproject/resources.

You can get the real path of your file using ServletContext#getRealPath

pakore
Yes, I am doing this. But in tomcat/webapps/myproject there is not src, just folders as pages, styles...
mujer esponja
where do you have your java code?
pakore
I eddited my question
mujer esponja
but that is exactly what I want to avoid, absoulte paths as /var/lib/tomcat6/webapps/myproject/resources. I want to get this resources via localhost or other host, to don't depend on the machine. Thanks
mujer esponja
That's why you should use `ServletContext#getRealPath` so you don't depend on the machine!
pakore
Thank you very much!
mujer esponja
Np, you are welcome.
pakore