Bonjour à tous,
J'ai un petit souci avec l'externalisation du fichier .properties de mon application.
En effet, je ne sais pas comment faire et toutes les techniques que je trouve sur le net ne marche pas dans mon cas.
Je précise que cela fonctionne très bien lorsque mon fichier .properties est inclus à l'application (au war).
Mon code java qui récupère le fichier .properties :
Cette méthode est appelée avec le paramètre suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 /** * @param pPropertiesFilePath * that may be : * - absolute in the file system including .properties extension * - relative in the classpath and without .properties extension * @return a Hashtable with the keys/values of the properties file */ public Hashtable getValuesFromProperties(String pPropertiesFilePath) throws FileNotFoundException { Hashtable results = new Hashtable(); foundInFileSystem = false; foundInClassPath = false; // Look first in the file system try { Properties sysfileProperties = new Properties(); sysfileProperties.load(new FileInputStream(pPropertiesFilePath)); foundInFileSystem = true; results.putAll(sysfileProperties); } catch (FileNotFoundException e) { // May safety be ignored, try next to read from classpath } catch (IOException e) { // May safety be ignored, try next to read from classpath } // If not found, look in the classpath if (!foundInFileSystem) { try { ResourceBundle classpathBundle = ResourceBundle.getBundle(pPropertiesFilePath); foundInClassPath = true; Enumeration enumKeys = classpathBundle.getKeys(); while (enumKeys.hasMoreElements()) { String key = (String) enumKeys.nextElement(); results.put(key, classpathBundle.getString(key)); } } catch (MissingResourceException e) { // May safety be ignored, appropriate exception will be thrown later } } if (foundInFileSystem || foundInClassPath) { return results; } else { throw new FileNotFoundException("Following file was not found neither in filesystem nor in classpath : " + pPropertiesFilePath); } }
String properties = "com/boite/projet/app";
où "com", "boite" et "projet" sont des répertoires inclus et "app" le fichier .properties (app.properties).
Merci d'avance pour votre aide.
Thomas
Partager