我在类中有一个函数来读取配置文件。当我自己运行它时,它会输出(使用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 :(得分: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