2017-06-15 75 views
0

閱讀了關於該主題的大約50個不同的主題後,我無法使用任何解決方案。從JAR Maven項目中訪問屬性文件

我想要做的是在jar中訪問我的config.properties文件。

我的項目是一個Maven項目和架構如下:

  • 我的項目
    • 的src/main/java的
      • 對myApp
        • MyClass.java
    • 的src/main /資源
      • icon.jpg
      • config.properties

所以,當我嘗試從Eclipse中訪問其工作原理與完美的此代碼:

InputStream configFile = MyClass.class.getResourceAsStream("config.properties"); Followed by more code here to retrieve properties...(that works)

但是,當我將項目導出到可運行JAR時,它不再工作。罐子裏面,文件如下:

  • File.jar
    • MyApp的
      • files.class這裏
      • 等...
    • META-INF
    • 資源
      • icon.jpg
      • config.properties
+0

安置自己的'pom.xml'文件 – freedev

+0

裏面的''部分你你的build jar文件中有真正的'resources'文件夾嗎?如果是,我假設你的配置/目錄佈局或你的pom配置是錯誤的... – khmarbaise

+0

這是我的pom.xml ' 4.0.0 MYAPP MYAPP 0.0.1-SNAPSHOT MYAPP \t http:// localhost:9000 ' – retArdos

回答

0

試試這個:

InputStream configFile = MyClass.class.getResourceAsStream("resources/config.properties"); 
+0

JAR不起作用。我在eclipse中得到一個NullPointerException。 – retArdos

+0

嘗試使用前面的斜槓: 'InputStream configFile = MyClass.class.getResourceAsStream(「/ resources/config.properties」);' –

+0

仍然不起作用。我想我已經嘗試了各種可能性,但它不起作用。 – retArdos