Getting a config file outside of a jar file? - java

I want to open a .config file outside of the .jar file so that I can use its properties inside of the .jar. How would I do this?

For clearance you will use a config file from the (executable) jar like this:
InputStream in = this.getClass.getResourceAsStream("config.properties");
Properties p = new Properties();
p.load(in);
that loads config.properties relative to the class of the current object.
For a config outside you propably use a file:
InputStream in = new FileInputStream(System.getProperty("user.dir")+"/"+"config.properties");
Properties p = new Properties();
p.load(in);

Use this.
String path = System.getProperty("user.dir")
path += "/config/myApp.properties"
Now you have the path of your properties file.
You know what to do next

Related

Externalize a property file which is inside a jar file

I have jar with application.properties file as below-
BASE_ENVIRONMENT = http://localhost
This is my utility class to read the property file
Properties prop = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream in = loader.getResourceAsStream(fileName + ".properties");
the property file is in <default package>
This jar is used as a dependent jar in my web application which is deployed in tomcat server.
But i need to change the BASE_ENVIRONMENT for production environment.
Is there a way i can externalize this property file value?
You could add a system parameter to act as a profile:
// default to DEV
String profile = "dev"
if (System.getProperty("ENV") != null) {
profile = System.getProperty("ENV");
}
Properties prop = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream in = loader.getResourceAsStream(fileName + "_" + profile + ".properties");
Then you would start your app with
.... -DENV=prod
and the file like config_prod.properties would be found in the classpath, the default would be config_dev.properties.

how to read a file from resources folder in properties file

I have a file under resources folder src/test/resources/file.xml
and another under src/test/resources/test.properties. I want to set a property in properties file to point to file.xml. How can I achieve this?
Say I have a property
test.file = file.xml
and my java class reads the file as follows:
File cert = new File(fileName); // fileName is the value of test.file
This does not work however.
You can use Properties class to read and write to config files.
Firstly, you need to find the relative path for the resources using
below steps
Secondly, you can configure and load the test properties file
Finally, read the property value and append with the resource
directory
Code:
String rootDirectory=System.getProperty("user.dir");
String resourceDirectory=rootDirectory+"src/test/resources/";
//Configure property File
Properties properties = new Properties();
properties.load(new FileInputStream(resourceDirectory+"test.properties"));
PropertyConfigurator.configure(properties);
//To get the property value
String tempFileName=properties.getProperty("test.file");
//filename Needs to be changed as below
File cert = new File(resourceDirectory+tempFileName);

read properties file

propsdatabase = new Properties();
try
{
// load a properties file
InputStream dbin = getClass().getResourceAsStream("/database.properties");
//props.load(in);
propsdatabase.load(dbin);
}
catch (IOException ex)
{
ex.printStackTrace();
}
I Want to access a file which is out of the jar file. just in the location of the jar file.
I am using a properties file named database.properties in main/resources folder .
The requirement is to change the location of database.properties outside the jar.
So I need something where the path can point to the location where the jar is .
I am using maven to build my jar
FileInputStream dbin = new FileInputStream("/my/folder/database.properties");
You can do it in two ways by defining absolute path for the pro
Class.getResourceAsStream ("/some/pkg/resource.properties");
And
ResourceBundle.getBundle ("some.pkg.resource");

JAVA: FileInputStream and FileOutputStream

I have this strange thing with input and output streams, whitch I just can't understand.
I use inputstream to read properties file from resources like this:
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream( "/resources/SQL.properties" );
rop.load(in);
return prop;
It finds my file and reds it succesfully. I try to write modificated settings like this:
prop.store(new FileOutputStream( "/resources/SQL.properties" ), null);
And I getting strange error from storing:
java.io.FileNotFoundException: \resources\SQL.properties (The system cannot find the path specified)
So why path to properties are changed? How to fix this?
I am using Netbeans on Windows
The problem is that getResourceAsStream() is resolving the path you give it relative to the classpath, while new FileOutputStream() creates the file directly in the filesystem. They have different starting points for the path.
In general you cannot write back to the source location from which a resource was loaded, as it may not exist in the filesystem at all. It may be in a jar file, for instance, and the JVM will not update the jar file.
May be it works
try
{
java.net.URL url = this.getClass().getResource("/resources/SQL.properties");
java.io.FileInputStream pin = new java.io.FileInputStream(url.getFile());
java.util.Properties props = new java.util.Properties();
props.load(pin);
}
catch(Exception ex)
{
ex.printStackTrace();
}
and check the below url
getResourceAsStream() vs FileInputStream
Please see this question: How can I save a file to the class path
And this answer https://stackoverflow.com/a/4714719/239168
In summary: you can't always trivially save back a file your read from the classpath (e.g. a file in a
jar)
However if it was indeed just a file on the classpath, the above answer has a nice approach

Spring MVC:How to get file path into Controller?

I have following folder structure:
ProjectFolder/images/some images
In the same folder
ProjectFolder/WEB-INF/classes/com/xyz/here is java file of controller.
How can I get the image path in the controller?
Please, help.
Thanks :)
If its a web context may be something like this could help
InputStream is = null ;
is = request.getSession().getServletContext().getResourceAsStream("/images/someimage.jpg");
or may be something like this:
InputStream is = null ;
String realPath = request.getSession().getServletContext().getRealPath("/images/someimage.jpg");
is = new FileInputStream(realPath);
You can store your image path in properties file.
store that property file in your classpath.
now access that property in your controller class like this:
Properties properties = new Properties();
/*to access your filename.properties file */
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));
String sServerLocation = properties.getProperty("server.upload.docs.path");
note that you should use escape charater in your property file like :
server.upload.docs.path=D:\\JDIS3\\DOCS\\

Categories

Resources