从Java到Tomcat路径的访问

从Java到Tomcat路径的访问,java,tomcat,configuration,path,Java,Tomcat,Configuration,Path,我在本地的Tomcat上运行了一个Web应用程序。路径是:tomcat/webapps/myproject 我在这个项目中有一些资源,它们都在一个文件夹中,路径是:tomcat/webapps/myproject/resources 因此,我试图使用文件config.properties从Java项目访问这些资源。在这个文件中,我有如下内容: tomcat.url= http://localhost tomcat.port=8080 tomcat.resources=/myproject/res


我在本地的Tomcat上运行了一个Web应用程序。路径是:
tomcat/webapps/myproject

我在这个项目中有一些资源,它们都在一个文件夹中,路径是:
tomcat/webapps/myproject/resources

因此,我试图使用文件
config.properties
从Java项目访问这些资源。在这个文件中,我有如下内容:

tomcat.url= http://localhost
tomcat.port=8080
tomcat.resources=/myproject/resources
我还尝试了
/
\
的不同组合,但在运行我的项目时出现此错误:

Trying to acces to a directory that does not exist
我的Java代码:

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");                  
} 
我不知道出了什么问题,在我以前做的项目中,与此类似,我有类似的东西,它发现了一切。
有什么想法吗??提前感谢。

文件必须位于
tomcat/webapps/myproject/src/main/resources
检查更新

我不知道您是如何访问该文件的,但以防我建议您使用

已编辑

问题是,当您的文件位于(可能是)
/var/lib/tomcat6/webapps/myproject/resources
时,您试图打开位于
/myproject/resources
的文件(请记住“/”是您的根文件系统)


您可以使用

获取文件的真实路径是的,我正在这样做。但在tomcat/webapps/myproject中没有src,只有作为页面、样式的文件夹……但这正是我想要避免的,绝对路径为/var/lib/tomcat6/webapps/myproject/resources。我希望通过localhost或其他主机获取这些资源,以不依赖于机器。谢谢这就是为什么你应该使用
ServletContext#getRealPath
这样你就不需要依赖机器了!好的,我重新措辞我的问题。在哪里保存Java代码?基本上你的webapp的文件夹结构是什么。我想做的项目只是为我的webapp项目提供服务。但这真的很有意思吗??我的意思是,我正在连接config.properties中的本地主机,对吗?谢谢你的建议和回答
 private static final String CONFIGURATION_PATH = "config.properties";
 PropertiesConfiguration configuration = new PropertiesConfiguration(CONFIGURATION_PATH);