2010-10-07 113 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