无法在可运行的 jar 中读取 java 6 中的属性文件 [英] cannot read a property file in java 6 in a runnable jar

查看:63
本文介绍了无法在可运行的 jar 中读取 java 6 中的属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Java 做一个小项目.使用的版本是带有eclipse Indigo的JRE6.

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

然后第一个问题是关于两个罐子,我必须颠倒才能使它们工作.(2 个 xstream 罐子)

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

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

如果有人有想法?

 protected Properties readPropertiesFile(final String filename,final int level){如果(文件名 == 空){返回空;}//如果第一级(0)则清除已加载文件的列表如果(级别== 0){this.alreadyLoadedFiles.clear();}InputStreamReader 流 = null;尝试 {//尝试打开与属性文件的连接stream = new InputStreamReader(new FileInputStream(filename), "UTF-8");} catch (FileNotFoundException e) {//尝试在Classpath中查找指定的属性文件this.logServices.severe("找不到'"+文件名+"'属性文件.");} catch (UnsupportedEncodingException e) {//TODO 自动生成的 catch 块this.logServices.severe("UnsupportedEncodingException '"+filename+"' UTF-8 中的属性文件编码");}//读取属性文件Properties props = new Properties();尝试 {props.load(stream);//将文件添加到已加载文件列表中this.alreadyLoadedFiles.add(文件名);} 捕获(异常 e){道具 = 空;this.logServices.severe("无法读取'"+文件名+"'属性文件:"+e.getMessage());} 最后 {尝试 {流.关闭();} catch (IOException e) {}}//在属性文件中搜索include标签this.readIncludePropertiesFiles(props, (level+1));返回道具;

解决方案

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

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

I'm working on a little big project in java. The version used is JRE6 with eclipse Indigo.

the application seems to work fine, but when i want to execute the runnable jar of this api, it doesn't work. So I execute my jar with c:...\jr6\bin\java.exe -jar c:\User\Olivier\Desktop\appli.jar

And then the first probleme was about two jars i have to invert to make them work. (2 xstream jars)

Now, a new error appears. It seems the application can't load a file name language.properties

i add it in the jar, with other jars, in the folder of the appli.jar, i also tried to add it in the manifest. I'm obviously unable to solve this problem by myself.

If someone have an idea?

 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;

解决方案

A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname.

By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked. Source JavaDoc

So if you put your file in a jar file the path might not be resolved depending on the directory where you are running your program. So the better way to access the file in a classpath is to use it as follows:

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

instead of

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

Eidt:

Here is an example screenshot of the structure of the maven project that works as described above. The file Lexicon.txt will be copied to the root of the jar file, therefore the name you pass to the getResourceAsStream() method is /Lexicon.txt as filename.

这篇关于无法在可运行的 jar 中读取 java 6 中的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆