簡體   English   中英

如何使用java從e​​clipse獲取當前項目路徑

[英]How to get the current project path from eclipse using java

我在eclipse中有一個名為SampleProj的項目。

項目結構如下:

SampleProj
   |  
    -- META-INF
            |
             -- dbdetails.properties

目前,我正在讀這樣的文件:

FileReader reader = new FileReader("C://Workspace//SampleProj/META-INF//dbdetails.properties");

但是這條路徑是我系統rit的本地路徑。 所以,我想讓這條路徑變得通用。

所以,我試圖保持這樣的道路:

FileReader reader = new FileReader("SampleProj/META-INF//dbdetails.properties");

但是,我發現文件未找到異常。

如何從Project中讀取此文件。 任何人都可以幫我解決這個問題......

您可以使用以下方法進

String workingDir = System.getProperty("user.dir");

然后你可以這樣做:

FileReader reader = new FileReader(workingDir +"//SampleProj//META-INF//dbdetails.properties");
  • 轉到運行 / 運行配置...
  • 選擇要使用的啟動配置
  • 選擇參數選項卡
  • 在“ 工作目錄”部分中,單擊“ 其他”
  • C:\\ Workspace放在文本字段中
  • 單擊“ 應用”

然后SampleProj/META-INF/dbdetails.properties應該作為路徑,因為相對路徑是相對於與System屬性user.dir對應的用戶工作目錄。

就像您的開發一樣,您可以像下面這樣做,但是一旦部署項目,最好在類路徑中添加文件

String currentDirectory=System.getProperty("user.dir");


FileReader reader = new FileReader(currentDirectory+"/SampleProj/META-INF//dbdetails.properties");

如果您使用spring web-mvc項目使用此示例代碼:

String path=session.getServletContext().getRealPath("/ui_resources/images/logo.png");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM