【问题标题】:Use property file in a JAR在 JAR 中使用属性文件
【发布时间】:2013-04-04 03:33:55
【问题描述】:

在看了很多属性文件文章和 cmets 之后,我真的迷路了。

我想要的只是检索值并覆盖它们 - 我想将它与 jar 文件一起使用。

如果我在 Eclipse 中编译它可以完美运行,但是在我编译的那一刻,我得到了著名的“找不到属性文件”-异常。

FileInputStream in = new   FileInputStream(ClassLoader.getSystemResource("client.properties").getPath()); 
Properties props = new Properties();
        props.load(in);
        in.close();

我得到的异常如下:

C:\Users\thomas\Desktop>java -jar erp_auer_client_v0_1.jar
java.io.FileNotFoundException: file:\C:\Users\thomas\Desktop\erp_auer_client_v0_
1.jar!\client.properties (Die Syntax f³r den Dateinamen, Verzeichnisnamen oder d
ie Datentrõgerbezeichnung ist falsch)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at global_functions.helperFunctions.getPathBARTENDEREXE(helperFunctions.
java:361)
        at client.programmeinstellungen.ProgrammEinstellungenManagement.<init>(P
rogrammEinstellungenManagement.java:59)
        at client.main.MainOverview$11.mousePressed(MainOverview.java:275)

德语部分(Die Syntax f³r den Dateinamen, Verzeichnisnamen oder d ie Datentrõgerbezeichnung ist falsch) 表示“文件名、目录名或磁盘名的语法错误”。

你知道那可能是什么吗?

【问题讨论】:

  • 你想把你的属性文件放在一个罐子里(没问题)。接下来你想在这个属性文件中保存值,对吧?我不确定这是否可能,因为 jar 文件是一个自包含文件,您的应用程序应该在该自包含文件中运行。我认为您只能从 Eclipse 运行,因为 Eclipse 是从存储项目的文件夹而不是从真正的 jar 文件中运行的......这有意义吗?
  • 感谢您的快速答复。但如果是这样,我可以如何/在哪里永久存储值并可以再次更改它?
  • jar 文件之外的任何地方:(1)您可以对给定位置进行硬编码; (2) 您可以假设您的属性文件将与您的 jar 文件位于同一文件夹中; (3) 您可以添加一个带有文件位置的参数并使用它,例如:java -jar erp_auer_client_v0_1.jar -filePath c:\temp\client.properties
  • 由于文件是通过双击 jar 文件启动的,所以 (2) 是最好的。如果要导出 jar 程序供用户使用,通常正确的方法是什么?
  • 我使用 maven,但如果您使用的是 Eclipse,您可以将其导出为 jar 文件并按照导出向导进行操作。应该没问题的。

标签: properties jar filenotfoundexception


【解决方案1】:

好的,我自己尝试了一下,发现它比我最初想象的要复杂一些。但是,我认为在您的场景中,您可以使用类似下面的内容。我在 windows 和 linux 中测试过,对我来说效果很好:

package mypackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Properties;

public class MyMainClass {

    public static void main(String[] args) throws URISyntaxException {
        /*
         * 
         * MyMainClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
         * In Linux it returns null
         * 
         * ClassLoader.getSystemClassLoader().getResource(".").getPath() + "MyProperty.properties";
         * In Linux it returns {JRE_PATH}/lib/ext/pulse-java.jar
         * In Windows it returns {JAR_FILE_PATH}
         */

        String propertyFilePath = "MyProperty.properties";
        Properties props = new Properties();
        try {
            FileInputStream in = new   FileInputStream(propertyFilePath); 
            props.load(in);
            in.close();
        } catch (Exception e) {
            File f = new File(propertyFilePath);
            System.err.println("Could not read file: " + f.getAbsolutePath());
        }

        System.out.println("Fetching property value of [now]: " + props.get("now"));
        String now = new Date().toString();
        System.out.println("Storing property [now]: " + now);
        props.setProperty("now", now);

        try {
            OutputStream out = new FileOutputStream(propertyFilePath); 
            props.store(out, "Saving value of [now]");
        } catch (Exception e) {
            File f = new File(propertyFilePath);
            System.err.println("Could not write file: " + f.getAbsolutePath());
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-02
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 2014-01-29
    相关资源
    最近更新 更多