[英]Is there a way to load information from a properties file that is outside a project's directory when the project gets exported as a JAR?
I need to keep database credentials in a properties file that is located outside of my project's folder/structure/class paths (ie C:/Parent_name/config/config.properties).我需要将数据库凭据保存在位于项目文件夹/结构/类路径之外的属性文件中(即 C:/Parent_name/config/config.properties)。 The project (C:/Parent_name/Tomcat/Project_name/) gets exported as a JAR and is used in other applications (C:/Tomcat/webapps/app_1/WEB-INF/lib/Project_name.jar) that are then deployed to a Tomcat server.项目 (C:/Parent_name/Tomcat/Project_name/) 导出为 JAR 并用于其他应用程序 (C:/Tomcat/webapps/app_1/WEB-INF/lib/Project_name.jar),然后部署到Tomcat 服务器。
I've found I can't do a simple FileInputStream because the file would need to be a part of the class path and I need the file to be outside of the project so DBA's can easily update credentials when needed.我发现我不能做一个简单的 FileInputStream 因为该文件需要成为 class 路径的一部分,并且我需要该文件位于项目之外,以便 DBA 可以在需要时轻松更新凭据。 getResourceAsSteam() returns a null pointer exception. getResourceAsSteam() 返回 null 指针异常。
Here's two examples of code that don't achieve what I need:这是两个没有达到我需要的代码示例:
FileInputSteam文件输入蒸汽
private static Object readConfigFile() {
try (InputStream input = new FileInputStream("C:\\Parent_name\\config\\config.properties")) {
Properties prop = new Properties();
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("db.url"));
System.out.println(prop.getProperty("db.user"));
System.out.println(prop.getProperty("db.password"));
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
getResrouceAsSteam() getResrouceAsSteam()
private static Object readConfigFile() {
try {
Class cls = Class.forName("SQLCredentials");
// returns the ClassLoader object associated with this Class
ClassLoader cLoader = cls.getClassLoader();
InputStream input = cLoader.getResourceAsStream("C:/Parent_name/config/config.properties");
Properties prop = new Properties();
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("db.url"));
System.out.println(prop.getProperty("db.user"));
System.out.println(prop.getProperty("db.password"));
} catch (IOException | ClassNotFoundException ex) {
ex.printStackTrace();
}
return null;
}
Yes there is a way to "Is there a way to load information from a properties file that is outside a project's directory when the project gets exported as a JAR?"是的,有一种方法可以“当项目导出为 JAR 时,是否可以从项目目录之外的属性文件中加载信息?”
The project being exported as a JAR has no bearing on the ability to read a regular file in the file system as VGR pointed out in his very helpful comment.正如 VGR 在他非常有用的评论中指出的那样,作为 JAR 导出的项目与读取文件系统中的常规文件的能力无关。
My problem was the file not being found.我的问题是找不到文件。 The debug step I took to lead me to the correct answer was listing all files in a directory我引导我找到正确答案的调试步骤是列出目录中的所有文件
String[] pathnames;
// Creates a new File instance by converting the given pathname string
// into an abstract pathname
File f = new File("C:\\Parent_name\\config");
// Populates the array with names of files and directories
pathnames = f.list();
// For each pathname in the pathnames array
for (String pathname : pathnames) {
// Print the names of files and directories
System.out.println(pathname);
}
Here I discovered that config.properties was actually config.properties.txt.在这里我发现 config.properties 实际上是 config.properties.txt。
private static Object readConfigFile() {
try (InputStream input = Files.newInputStream(Paths.get("C:\\Parent_name\\config\\config.properties"))) {
Properties prop = new Properties();
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("dev.url"));
System.out.println(prop.getProperty("dev.user"));
System.out.println(prop.getProperty("dev.password"));
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.