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;
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:
instead of
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 thegetResourceAsStream()
method is/Lexicon.txt
asfilename
.