【问题标题】:cannot read a property file in java 6 in a runnable jar无法在可运行 jar 中读取 java 6 中的属性文件
【发布时间】:2016-07-20 14:23:19
【问题描述】:

我正在用 java 做一个小的大项目。使用的版本是 JRE6 with eclipse Indigo。

应用程序似乎工作正常,但是当我想执行这个 api 的可运行 jar 时,它不起作用。 所以我用 c:...\jr6\bin\java.exe -jar c:\User\Olivier\Desktop\appli.jar 执行我的 jar

然后第一个问题是关于两个罐子我必须倒置以使它们工作。 (2 个 xstream 罐子)

现在,出现了一个新错误。应用程序似乎无法加载文件名 language.properties

我将它与其他 jars 一起添加到 appli.jar 的文件夹中,我也尝试将它添加到清单中。我自己显然无法解决这个问题。

如果有人有想法?

 protected Properties readPropertiesFile(final String filename,final int level){
    if (filename == null) {
        return null;
    }
    //if first level (0) then clear the list of already loaded files
    if (level == 0) {
        this.alreadyLoadedFiles.clear();
    }


    InputStreamReader stream = null;
    try {
        //Try to open a connection with the properties file

        stream = new InputStreamReader(new FileInputStream(filename), "UTF-8");

    } catch (FileNotFoundException e) {
        //Try to find the specified propertie file in Classpath
        this.logServices.severe("Cannot found the '"+filename+"' properties file.");

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        this.logServices.severe("UnsupportedEncodingException '"+filename+"' properties file encoding in UTF-8");
    } 

    //Read the properties file
    Properties props = new Properties();
    try {

            props.load(stream);

        //Add the file in the list of already loaded file
        this.alreadyLoadedFiles.add(filename);

    } catch (Exception e) {
        props = null;
        this.logServices.severe("Cannot read the '"+
                filename+"' properties file : "+e.getMessage());
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
        }
    }
    //Search for the include Tag in properties file
    this.readIncludePropertiesFiles(props, (level+1));
    return props;

【问题讨论】:

  • 我展示了用户的属性系统,我试图把我所有的属性都放在里面,它就像这样工作。不幸的是,我希望将属性与 jar 链接而不是与系统链接,就像我采用我的解决方案一样,我不知道用户目录是什么。那么,是否有办法改变这一事实?

标签: java properties java-6 eclipse-indigo


【解决方案1】:

路径名,无论是抽象的还是字符串形式的,都可以是绝对的或相对的。绝对路径名是完整的,因为不需要其他信息来定位它表示的文件。相反,相对路径名必须根据从其他路径名获取的信息来解释。

默认情况下,java.io 包中的类总是根据当前用户目录解析相对路径名。此目录由系统属性 user.dir 命名,通常是调用 Java 虚拟机的目录。 Source JavaDoc

因此,如果您将文件放在 jar 文件中,则路径可能无法解析,具体取决于您运行程序的目录。因此,在类路径中访问文件的更好方法是按如下方式使用它:

InputStream is = this.getClass().getResourceAsStream(filename);
InputStreamReader stream =  new InputStreamReader(is, "UTF-8");

而不是

stream = new InputStreamReader(new FileInputStream(filename), "UTF-8");

开斋节:

这里是 maven 项目结构的示例屏幕截图,其工作方式如上所述。 Lexicon.txt 文件将被复制到 jar 文件的根目录下,因此您传递给 getResourceAsStream() 方法的名称是 /Lexicon.txt 作为 filename

【讨论】:

  • 了解这些信息。但是,getClass().getRessourceAsStream(filename) 是一样的。除非我在 user.dir 中添加属性文件,否则应用程序仍未在同一目录中找到该文件
  • 我不知道你把文件放在哪里了。如果该文件位于 jar 文件的根目录中,请将 /filename 传递给 getResourceAsStream() 方法。如果它在另一个位置,只需使用/dir1/dir2/../filename。如果文件与读取它的类在同一目录下,名称filename就足够了。
  • 它们与 jar 位于同一目录中,但 jar 看不到该文件。所以为了解决这个失败,我用 System.getProperty("java.class.path").replace("myApp.jar","")+filename 重命名文件名 这是最糟糕的解决方案...
  • 我编辑了帖子并添加了截图。查看目录和包的结构。目录src 包含所有源文件和资源。
猜你喜欢
  • 2014-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
  • 2020-02-27
  • 2019-04-23
  • 2013-10-25
相关资源
最近更新 更多