2010-10-07 116 views
0


我有一个webapp在我的本地Tomcat中运行。路径是:tomcat/webapps/myproject
我都在这个路径中的文件夹,在这个项目中的一些资源:tomcat/webapps/myproject/resources从Java访问Tomcat路径

所以,我从一个Java项目试图查看在此资源,使用文件config.properties。在这个文件中我有这样的事情:

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

我tryed也有不同的/\组合,但我得到这个错误运行我的项目:

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");     
} 

我不知道什么是错的,在我之前制作的项目中,似乎是这样,我有类似的东西,它找到了一切。
任何想法??提前致谢。

+0

好你的文件的真实路径,我改一下我的问题。你在哪里保存你的Java代码?基本上什么是你的web应用程序的文件夹结构。 – pakore 2010-10-07 09:17:04

+0

我想要这样做的项目只是为我的webapp项目提供服务。但它真的是啥?我的意思是,我在config.properties中连接到本地主机,对吗?感谢您的commnents和答案 – 2010-10-07 09:24:33

+0

检查我的更新。 – pakore 2010-10-07 09:46:10

回答

1

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

我不知道你是如何访问该文件,但以防万一,我建议你使用PropertiesConfiguration

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

编辑

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

你可以使用ServletContext#getRealPath

+0

是的,我正在这样做。但在tomcat/webapps/myproject中没有src,只是文件夹为页面,样式... – 2010-10-07 08:31:16

+0

你有哪些java代码? – pakore 2010-10-07 08:37:39

+0

我编辑了我的问题 – 2010-10-07 08:45:52