I have a problem with an exported jar file. When I run my project in Eclipse it runs fine, but when I run it as an exported jar from the console I receive the following error message:
java.io.FileNotFoundException: firstLaunch.properties (System can't find file)
or
java.io.FileNotFoundException: resources/config/firstLaunch.properties (System can't find file)
I tried to put it in to the resource folder and change syntax from firstLaunch.properties to /resource/config/firstLaunch.properties, but again it says the same thing but with a different path. I don't know why is this doing this.
Here is the code:
public void saveConfigFile(String file, String key, String value) {
Properties prop = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream(file);
// set the properties value
prop.setProperty(key, value);
// save properties to project root folder
prop.store(output, null);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
My syntax for executing the method is
if (properties.loadConfigFile("firstLaunch.properties", "value").equals(properties.loadConfigFile("true.properties", "true"))) {
properties.saveConfigFile("port.properties", "port", "8795");
properties.saveConfigFile("ip.properties", "ip", temp[1]);
properties.saveConfigFile("firstLaunch.properties", "value", "false");
settings.port = properties.loadConfigFile("port.properties", "port");
settings.myIp = properties.loadConfigFile("ip.properties", "ip");
} else {
settings.port = properties.loadConfigFile("port.properties", "port");
settings.myIp = properties.loadConfigFile("ip.properties", "ip");
}
Your problem probably has to do with how you are referencing the file location. Add some detail/code examples as to how you are referencing the code, so then we can be sure to help. Having said that here is another way to reference a properties file:
Put it in the classpath like this:
private static Properties prop = new Properties();
private static String filename = "<name of file>.properties";
InputStream input = <ClassName>.class.getClassLoader().getResourceAsStream(filename);
try {
if (input==null) {
loggerOut.log(Level.SEVERE, "Sorry, unable to find " + filename);
}
prop.load(input);
loggerOut.info("XML In storage path: " prop.getProperty("<property in file>"));
fileNameAndPath = prop.getProperty("fileNameAndPathIN").trim();
logNameAndPath = logPath + logName;
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input!=null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related
I am trying to read some property from the properties file, but my code is not reading the property file. The properties file is in some folder in my machine.
Here is my code:
public String getproperty(){
String extension="";
Properties prop = new Properties ();
String propname = "\\"+Any location in your machine+"\\fileExtension.properties";
Logger.debug("ReadFiles", " ----Property file path---- "+ propname, null);
ip = getClass().getClassLoader().getResourceAsStream(propname);
Logger.debug("ReadFiles", " ----ip value ---- "+ip, null);
try {
if(ip != null){
prop.load(ip);
Logger.debug("ReadFiles", " ----Property file loaded---- ", null);
}
extension = prop.getProperty("fileExt");
Logger.debug("ReadFiles", " ----Property = " + extension, null);
} catch (IOException e) {
Logger.error("ReadFiles", " ----Error while loading property file---- ", null);
e.printStackTrace();
}
finally{
try {
ip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return extension;
}
I am creating a jar placing it in lib folder of server (installed in my machine) and placing the properties file in my machine and given the same path in code. I have tried with absolute path and without absolute path.
Please try this example that uses an absolute path to the properties file.
package com.company;
import java.io.*;
import java.util.Properties;
public class Main {
public static void main(String[] args) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("/home/dac/gs-rest-service/javacode/src/main/java/com/company/config.properties");
prop.load(input);
String extension = prop.getProperty("fileExt");
System.out.println("ReadFiles ----Property = " + extension);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Test
cat /home/dac/gs-rest-service/javacode/src/main/java/com/company/config.properties
#Mon Dec 26 17:31:30 CET 2016
dbpassword=password
database=localhost
dbuser=foobar
fileExt=.xml⏎
Run the program
ReadFiles ----Property = .xml
How can I save a prop file in a specific folder for example,
now it is saved in the root I guess, but it needs to be in the same folder as the class where it is created.
I also want to know how to load it. If it possible to load a properties file easily from the root then it is okay as well to save it in the root.
code creating the file, first 2 lines with // ( = make code work now without using prop file), class name = Providers
public static DataAccessProvider createProvider (URL url) {
//MovieDAOOnline mdaoOn = new MovieDAOOnline();
//mdaoOn.setUrl(url);
Properties prop = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream("config.properties");
// set the properties value
prop.setProperty("uri", url.toString());
prop.store(output, null);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return new OnlineProvider();
}
code for getting the file, first line in comment needs to be changed to get uri from propertie:
public Movie getMovie(int id) throws DataAccessException{
//StringBuilder builder = new StringBuilder(url.toString());
builder.append("movies.xml");
MovieConfigRead mcr = new MovieConfigRead();
List<Movie> film = null;
try {
film = mcr.geefMovies(builder.toString());
} catch (JAXBException e) {
throw new DataAccessException();
} catch (MalformedURLException e) {
throw new DataAccessException();
}
for (Movie movie : film) {
if (movie.getId() == id) {
return movie;
}
}
return null;
}
Here is my problem:
I write a util class Config.java:
public static Properties getProperties(String filename, Class clazz) {
Properties prop = new Properties();
InputStream ins = null;
try {
if (clazz == null) {
ins = ClassLoader.getSystemResourceAsStream(filename);
} else {
ins = clazz.getResourceAsStream(filename);
}
logger.info("load properties with filename: " + filename);
if (ins != null) {
prop.load(ins);
}
} catch (Exception ex) {
logger.error("Error while loading file: " + filename, ex);
} finally {
try {
if (ins != null) {
ins.close();
}
} catch (Exception ex) {
logger.error("Error while close inputstream: " + filename, ex);
}
}
return prop;
}
and I compile this code into a jar whose name is: gs-common.jar.
I also write a class named AnalysisDBService and invoke Config class as follows:
Properties properties = Config.getProperties("longKeywordRule.properties",AnalysisDBService.class);
and compile this biz project into jar:gs-biz.jar.
and I write a web applciation which dependends on gs-common and gs-biz(actually gs-common.jar and gs-biz.jar is located in /WEB-INF/lib).
but I just found the properties file in gs-biz.jar cannot be loaded when the web application start up in tomcat.
could somebody tell me how to do this?
I am creating a properties file and putting into my classpath folder Resources.
When I tried to read this file , i am not getting the expected result. i am getting the result of the previous values printed instead of the property values set now.
My class file is as follows :
public class App {
public static void main(String[] args) {
Properties prop = new Properties();
PrintWriter output = null;
try {
output = new PrintWriter("Resources/config.properties");
// set the properties value
prop.setProperty("database", "localhost");
prop.setProperty("dbuser", "mkyong");
prop.setProperty("dbpassword", "password");
// save properties to project root folder
prop.store(output, null);
if(output!=null) {
System.out.println("Output");
output.close();
}
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
output.close();
}
}
Properties prop1 = new Properties();
BufferedInputStream input = null;
try {
String filename = "config.properties";
input = (BufferedInputStream) AppCPLoad.class.getClassLoader().getResourceAsStream(filename);
if(input==null){
System.out.println("Sorry, unable to find " + filename);
return;
}
//load a properties file from class path, inside static method
prop1.load(input);
//get the property value and print it out
System.out.println(prop1.getProperty("database"));
System.out.println(prop1.getProperty("dbuser"));
System.out.println(prop1.getProperty("dbpassword"));
if(input!=null) {
System.out.println("Input");
input.close();
}
} catch (IOException ex) {
ex.printStackTrace();
} finally{
if(input!=null){
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Please help.
When you run the program, the properties file is loaded and the values are read. After you rewrite the properties file, that doesn't mean that the properties you have loaded already have be to rewritten. You need to reload the properties file and re-read the values. You are looking for an implementation like ReloadableResourceBundleMessageSource
How can I access a property file called test.property in user.home?
This doesn't work:
public class Properties {
private static java.util.Properties p = new java.util.Properties();
static {
try {
String home = System.getProperty("user.home");
home += "\\test.properties";
System.out.println("User home: " + home);
InputStream is = Properties.class.getResourceAsStream(home);
p.load(is);
} catch (FileNotFoundException e) {
System.out.println("property file could not be found!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
File propsFile = new File(home, "test.properties");
InputStream is = new FileInputStream(propsFile);
You will probably find that the user.home is not on the run-time class-path of the application. In that case, use a File instead.