gpt4 book ai didi

java - 在不同的类中调用函数时找不到文件异常

转载 作者:行者123 更新时间:2023-12-02 12:18:36 24 4
gpt4 key购买 nike

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

最佳答案

我已经重组了类和方法来进行快速测试,我能够很好地读取 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

关于java - 在不同的类中调用函数时找不到文件异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45972158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com