2016-10-12 55 views
-1
  1. 我在我的Java應用程序中使用了屬性文件並訪問靜態塊內部的屬性文件。

static { 

    try { 
    properties = new Properties(); 

properties.load(new FileInputStream("Test.properties")); 

     } catch (Exception ex) { 
      ex.printStackTrace(); 

    } 

創建並經由命令提示能夠訪問的場所,其中的jar文件位於目錄無法從目錄外部訪問屬性文件 - 運行Jar文件

示例文件運行jar文件中:E:\測試\ java的罐子所有MyApplication。 jar - >這是jar位於的目錄。這種方式工作正常

  • 我想運行從其他目錄這個jar文件等作爲

    示例:d:\結果--->其他目錄。現在我從這個目錄運行jar。

  • 我試着在提示符下運行這個命令。

    示例:d:\結果\ java的罐E:\測試\ MyApplication.jar

  • 我有個


  • java.io.FileNotFoundException: Test.properties (The system cannot find the file specified) 
    
         at java.io.FileInputStream.open0(Native Method) 
    
         at java.io.FileInputStream.open(Unknown Source) 
    
         at java.io.FileInputStream.<init>(Unknown Source) 
    
        at java.io.FileInputStream.<init>(Unknown Source) 
    
        at MyApplication.<clinit>(MyApplication.java:47) 
    
  • 如何從目錄外部運行該jar文件時訪問屬性文件?
  • +0

    這是從同一個目錄? n請分享項目層次結構和屬性文件的位置? –

    +0

    不,目錄不同。 –

    +0

    你是否看過Jar,它有'Test.properties'? – Vihar

    回答

    0

    您需要將託管Test.properties的文件夾添加到CLASSPATH中。 在這種情況下,應將E:\ test \添加到CLASSPATH中。