Sometimes it is really difficult to access any resources form different Java applications. So let's see some scenarios:
It is desired to have a property file outside the "jar file" so that we can change properties when needed.
For instance, we will place the properties file in the same folder that the "jar file" lies.
To access the folder where the jar is, the trick is to access the folder where the compiled classed are placed and scale 3 folders. (Oh! rather strange, if anybody knows a better idea.., he will be welcome).
Here is a simple code. The name of the class containing the code is ThisClass, and we have created a Property class that feeds with a file in the same folder that the" jar file"
WebContent/resources/data
For instance, let's create this file:
src/main/resources/data/AccesData.txt
We can access it with this code
The code is very similar to the previous, but a bit different.
1. Executable jar application in a NO WEB environment.
Take into account that this is not a JSF application !!!It is desired to have a property file outside the "jar file" so that we can change properties when needed.
For instance, we will place the properties file in the same folder that the "jar file" lies.
To access the folder where the jar is, the trick is to access the folder where the compiled classed are placed and scale 3 folders. (Oh! rather strange, if anybody knows a better idea.., he will be welcome).
Here is a simple code. The name of the class containing the code is ThisClass, and we have created a Property class that feeds with a file in the same folder that the" jar file"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * Gets the folder where resides the executable jar * @return */ public static String getJarFolderPath() { File f = new File(ThisClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()); String suffix=File.separator +".."; //jarFolder=f.getAbsolutePath()+ "/../../../"file.properties" ; String jarFolder=f.getAbsolutePath()+ StringUtils.repeat(suffix, 3) + File.separator; return jarFolder; } public static Properties getRelativeFromJarFileProperties(String filePath) throws FileNotFoundException, IOException { Properties properties = new Properties(); String myPath=getJarFolderPath()+filePath; System.out.println(myPath); properties.load(new FileInputStream(myPath)); for(Object o: properties.keySet()) System.out.println(o.toString() + "-->"+ properties.getProperty(o.toString())); return properties; } |
2. JSF application in NO MAVEN environment
In the Eclipse project, we can see a folder called WebContent. We want to create the following tree structure of folders and place some data in the data folder:WebContent/resources/data
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 | /** * Get an input stream from a relative path from WebContent folder in NO Maven Project * * If there is a file in the folder WebContent/resources/data/AccessData.txt we should call it as: * * Inputstream in=getStreamFromWebContentFolder("./resources/data/AccessData.text") * * @param filePath * @return */ public static InputStream getStreamFromWebContentFolder(String filePath) { return FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(filePath); } /** * Get the path from a relative path from WebContent folder in NO Maven Project * * If there is a file in the folder WebContent/resources/data/AccessData.txt we should call it as: * * String myFileName = getPathFromWebContentFolder("./resources/data/AccessData.text") * * @param filePath * @return * @throws MalformedURLException */ public static String getPathFromWebContentFolder(String filePath) throws MalformedURLException { return FacesContext.getCurrentInstance().getExternalContext().getResource(filePath).getPath(); } |
3. JSF MAVEN Application
In this case, a good option is to place a properties folder in the src/main/resources folder.For instance, let's create this file:
src/main/resources/data/AccesData.txt
We can access it with this code
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 | /** * Get an input stream from a relative path from resources folder in Maven Project * * If there is a file in the folder resources/data/AccessData.txt we should call it as: * * InputStream in=getStreamFromResourcesFolder("data/AccessData.text") * * @param filePath * @return */ public static InputStream getStreamFromResourcesFolder(String filePath) { return Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); } /** * Get the path from a relative path from resources folder in Maven Project * * If there is a file in the folder resources/data/AccessData.txt we should call it as: * * String path=getPathFromWebContentFolder("data/AccessData.text") * * @param filePath * @return * @throws MalformedURLException */ public static String getPathFromResourcesFolder(String filePath) { return Thread.currentThread().getContextClassLoader().getResource(filePath).getPath(); } |
The code is very similar to the previous, but a bit different.
Comments
Post a Comment