在 JAR 可执行文件中设置属性

问题描述 投票:0回答:2

我制作了一个程序,它使用多个类来执行各种任务。然而,每个类每次运行都需要引用相同的文件路径。因此,在我的 jar 可执行文件中,我有一个属性文件,它有 2 个属性:filePath 和 installStatus。当我第一次运行该程序时,它会测试 installStatus ,如果该状态为“false”,则系统会提示用户选择创建某些文件和文件夹的位置。我的 jar config.properties 中的默认值是“FileLocation=null”和“Install=false”。该属性文件(在 Eclipse 中)与我的类位于同一包中,以便于访问。当我访问配置来测试安装状态(以确保我已运行该程序一次并制作了必要的文件)时,我没有收到任何错误,并且程序继续正确安装。我用于查找这些属性的执行代码如下:

    prop.load(installationClass.class.getResourceAsStream("config.properties"));
        
        // If the "Install" property is false then that means this program
        // is being run for the first time to we go through the installation
        // process and if it isn't, move to the next class
        if(prop.getProperty("Install").equals("false")) {
            myObj.installLocation();
        } else {
            FileNotFoundGUI fnfGUI = new FileNotFoundGUI();
            fnfGUI.mainMethod();
        }

在此类执行结束时,我希望将属性更新为用户选择的文件路径并安装为 true。我这样做如下所示:

prop.setProperty("Install", "true");
filePath = filePath + "\\Tracking Program\\Text Files";
prop.setProperty("FileLocation", filePath);
FileWriter fw = new FileWriter("config.properties");
prop.store(fw, null);

但是,这会在与我的 jar 文件相同的文件位置创建一个新的 config.properties 文件,我需要一个 filePath 来导航到并打开该文件。为了在其他类中获取该 filePath,我必须访问属性文件来检索它们,当然他们不知道它在哪里。有没有办法可以写入打包在我的 jar 文件中的 config.properties 文件?

我已经解压了我的 jar 文件,以确保我的 config.properties 文件在那里,它确实是这样,并且它有我原来添加的内容。这些添加内容不会更新,因为它会生成新的属性文件。我尝试了各种方法来访问此属性文件,并尝试在 jar 文件中进行访问,但到目前为止还没有成功。任何建议或想法将不胜感激。

TLDR:我想访问、读取和写入存储在可执行 jar 文件中的属性文件。

java jar properties executable
2个回答
0
投票

再次感谢大家的宝贵帮助。这是我想出的解决方案,以防其他人想做和我一样的事情。

import java.io.File;
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Properties;
import java.io.FileReader;

public class PropertiesTest {
    public static void main(String[] args) throws IOException{
        
        // Creating first property value to ensure they work seperately
        // then making the directory that I wish to read from later
        // System.getProperty("user.home") leads to C:\Users\YourUser
        // so I am creating a file "Tracking Program" in C:\Users\YourUser
        Properties propertiesWrite = new Properties(); 
        File theDir = new File(System.getProperty("user.home") + "\\Tracking Program");
        theDir.mkdir();

        // This creates the config.properties file at the given destination
        // and will also allow us to write to it
        FileOutputStream fileOutputStream  = new FileOutputStream(System.getProperty("user.home") + "\\Tracking Program\\config.properties"); 
  
        // Creating two values (FileLocation and Install) and giving them
        // values (null and false)
        propertiesWrite.setProperty("FileLocation", "null"); 
        propertiesWrite.setProperty("Install", "false"); 
  
        // Storing the values we just created at fileOutputStream
        // and not leaving any comments within the file
        propertiesWrite.store(fileOutputStream,null); 
        
        // Closing fileOutputStream to avoid any memory leaks
        fileOutputStream.close(); 
        
        // Creating a new properties value to ensure no confusion
        // between the usage of the properties variables
        Properties propertiesRead = new Properties();
        
        // Creating a file reader that will allow us to read
        // from our properties file
        FileReader fr = new FileReader(System.getProperty("user.home") + "\\Tracking Program\\config.properties");
        
        // Loading the properties file so that we can actually 
        // retrieve data from it
        propertiesRead.load(fr);
        
        // Printing out one of the values to ensure we access the correct
        // file and that it was properly written to (Prints "false")
        System.out.println(propertiesRead.getProperty("Install"));
    }
}

0
投票

您最好使用

Reader
Writer
作为属性,因为它们为您提供了更大的灵活性,而且替代方案将您限制为“ascii”,这可能会产生令人惊讶和奇怪的副作用。尝试资源也是你的朋友。当您加载
Properties
时,流不会关闭。这应该更加严格,因为目录可能在那里,但有人可能已经删除了该文件。

import java.nio.file.Path;
import java.nio.file.Files;
import java.io.IOException;
import java.util.Properties;
import java.io.Reader;
import java.io.Writer;

public class PropertiesTest {
    public static final String CONFIG_FILE = "config.properties";

    public static void main(String[] args) throws IOException {

        // Creating first property value to ensure they work seperately
        // then making the directory that I wish to read from later
        // System.getProperty("user.home") leads to C:\Users\YourUser
        // so I am creating a file "Tracking Program" in C:\Users\YourUser
        Properties propertiesWrite = new Properties();
        // File theDir = new File(System.getProperty("user.home") + "\\Tracking
        // Program");
        Path configDir = Path.of(System.getProperty("user.home"), "Tracking Program");
        boolean firstRun = true;
        try {
            Files.createDirectory(configDir);
        } catch (IOException e) {
            firstRun = false;
        }

        if (firstRun) {
            // Creating two values (FileLocation and Install) and giving them
            // values (null and false)
            propertiesWrite.setProperty("FileLocation", "null");
            propertiesWrite.setProperty("Install", "false");

            // This creates the config.properties file at the given destination
            // and will also allow us to write to it
            try (Writer out = Files.newBufferedWriter(configDir.resolve(CONFIG_FILE))) {
                // Storing the values we just created at fileOutputStream
                // and not leaving any comments within the file
                propertiesWrite.store(out, null);
            }
        }

        // Creating a new properties value to ensure no confusion
        // between the usage of the properties variables
        Properties propertiesRead = new Properties();

        // Creating a file reader that will allow us to read
        // from our properties file

        try (Reader in = Files.newBufferedReader(configDir.resolve(CONFIG_FILE))) {
            // Loading the properties file so that we can actually
            // retrieve data from it
            propertiesRead.load(in);
        }
        // Printing out one of the values to ensure we access the correct
        // file and that it was properly written to (Prints "false")
        System.out.println(propertiesRead.getProperty("Install"));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.