简体   繁体   English

当项目导出为 JAR 时,有没有办法从项目目录之外的属性文件中加载信息?

[英]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 时,是否可以从项目目录之外的属性文件中加载信息?”

  1. 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 导出的项目与读取文件系统中的常规文件的能力无关。

  2. 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。

  1. In order to create a proper config.properties file, I created a new file in eclipse, named it config.properties, then copy/pasted it into C://Parent_name//config.为了创建正确的 config.properties 文件,我在 eclipse 中创建了一个新文件,将其命名为 config.properties,然后将其复制/粘贴到 C://Parent_name//config 中。 I used the following code to read the file and print it's values:我使用以下代码读取文件并打印它的值:
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.

相关问题 Java项目在Eclipse上工作但在导出到runnable jar文件时不起作用 - Java project working on Eclipse but not working when exported to runnable jar file 从Eclipse将Web项目导出为War文件时,无法导出非jar文件 - Unable to export non-jar files when web project is exported as a war file from Eclipse 将项目导出到 JAR 时,newKieSession() 返回 null - newKieSession() returns null when project is exported to JAR 从IDE加载.properties文件,也从JAR外部加载.properties文件 - Load .properties file from IDE and also from outside of JAR Git或外部项目中的属性文件? - Properties file in Git or outside project? Maven项目-从资源文件夹(项目根目录)外部加载文件 - Maven project - load file from outside of resources folder (project root) 如何将属性放在项目之外并同时加载属性项目属性和外部项目属性? - How can I put properties outside the project and load both properties project properties and outside project properties? 如何从Maven项目中的外部jar读取属性文件 - How to read properties file from external jar in maven project IllegalArgumentException:从cmd执行Maven项目的Jar时不是文件或目录 - IllegalArgumentException: Not a file or directory while executing Jar of a maven project from cmd 从msf4j项目运行jar文件时,“找不到或加载主类” - “Could not find or load main class” when running a jar file from a msf4j project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM