2017-08-31 26 views
0

我在類中有一個函數來讀取配置文件。當我自己運行它時,它會輸出(使用println)我需要的文件中的值。但是,當我在另一個類中使用這些值作爲數據庫連接的參數時,我得到一個未找到文件異常。在不同類中調用函數時發現文件異常

如果我從我的系統(即D:/File/HereIsTheFile/config.properties)設置絕對文件路徑,但如果我打包並部署,則完全無用。

下面是配置讀取器:

public class RetConfig { 
    public static String retConfig(String param) { 
     Properties prop = new Properties(); 
     try (InputStream input = new FileInputStream("resources/config.properties")) { 
      prop.load(input); 
      System.out.println(prop.getProperty("url")); 
      System.out.println(prop.getProperty("user")); 
      System.out.println(prop.getProperty("password")); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
     return prop.getProperty(param); 
    } 

    public static void main(String[] args) { 
     System.out.println(retConfig("url")); 
    } 
} 

這裏是另一個類的功能:

public static List<MyDataType> retSalesShipOem(String queryInput) throws SQLException { 
    List<MyDataType> dataList = new ArrayList<>(); 
    try (Connection conn = (Connection) DriverManager.getConnection(RetConfig.retConfig("url"), 
                    RetConfig.retConfig("user"), 
                    RetConfig.retConfig("password"))) { 
     try (Statement stmt = (Statement) conn.createStatement()) { 
      try (CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet();) { 
       crs.setCommand(queryInput); 
       crs.execute(conn); 
       conn.setAutoCommit(false); 
       while(crs.next()){ 
        /* 
        * 
        */ 
       } 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(System.out); 
    }  
    return dataList; 
} 

這裏是堆棧跟蹤(多錯誤,但所有類似)

java.io.FileNotFoundException: resources\config.properties (The system cannot find the path 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 kpiDBAccess.RetConfig.retConfig(RetConfig.java:15) 
    at kpiDBAccess.EnterD.retSalesShipCyo(EnterD.java:81) 
    at javascriptChartData.JSDCSales1C.formatChartDataCYO(JSDCSales1C.java:48) 
    at javascriptChartData.JSDCSales1C.doGet(JSDCSales1C.java:216) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) 
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) 
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) 
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) 
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Unknown Source) 
+0

哪裏是你的堆棧跟蹤? –

+0

更新了你的問題,如果這是一個堆棧跟蹤:)對不起,還有新 –

+0

謝謝。是的,這正是堆棧跟蹤的內容。這就是所謂的,因爲每個函數都向堆棧添加一個條目,所以沿着堆棧追溯會給你一系列調用,讓你到達崩潰的地方。 –

回答

1

我已經重組了類和方法來做快速測試,我可以閱讀resources/config.properties條目很好。

資源/ config.properties

url:www.google.com 
user:bob 
password:xxx 

讀取文件:

public class RetConfig {  
    String urlValue; 
    String userValue; 
    String pwdValue; 

    public void retConfig() { 
     Properties prop = new Properties(); 
     try (InputStream input = new FileInputStream("resources/config.properties")) { 
      prop.load(input); 
      this.urlValue = prop.getProperty("url"); 
      this.userValue = prop.getProperty("user"); 
      this.pwdValue = prop.getProperty("password"); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

調用#1類來訪問屬性。

public class CallRetConfig { 
    public static void callerMethod(){ 
     RetConfig retConfig = new RetConfig(); 
     retConfig.retConfig(); 
     System.out.println(" print in second class : "+retConfig.urlValue); 
    } 
    public static void main(String[] args) { 
     callerMethod(); 
    } 
} 

輸出:

print in second class : www.google.com 
+0

我試過這個,它工作,它看起來像我需要檢查我的其他類中的代碼,並確切地看到它爲什麼找不到文件路徑。 –

+0

@ E.Ancuta:如果這有幫助,請接受答案。謝謝你,祝你好運。 –

+0

它回答了我的問題,但那是因爲我不知道當我做了什麼問題。事實證明,如果我在當前存在的類中調用該函數,那麼獲取配置文件的文件路徑時出現問題,但是如果我創建一個新類,它就可以正常工作。這可能是一個包裹問題,但我會接受你的答案 –