cannot read a property file in java 6 in a runnable jar

℡╲_俬逩灬. 提交于 2019-12-25 07:19:11

问题


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;

回答1:


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.



来源:https://stackoverflow.com/questions/38483934/cannot-read-a-property-file-in-java-6-in-a-runnable-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!