Access from Java to Tomcat paths

谁说我不能喝 提交于 2019-12-12 03:57:19

问题



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 running 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:


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

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

 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



来源:https://stackoverflow.com/questions/3879815/access-from-java-to-tomcat-paths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!