Reputation: 2628
I am not able to load property file using below snippet
URL configURL = null;
URLConnection configURLConn = null;
InputStream configInputStream = null;
currConfigProperties = new Properties();
try {
String configPropertiesFile = getParameter("propertiesFile");
if (configPropertiesFile == null) {
configPropertiesFile = "com/abc/applet/Configuration.properties";
}
System.out.println("configPropertiesFile :"+configPropertiesFile);
configURL = new URL(getCodeBase(), configPropertiesFile);
configURLConn = configURL.openConnection();
configInputStream = configURLConn.getInputStream();
currConfigProperties.load(configInputStream);
} catch (MalformedURLException e) {
System.out.println(
"Creating configURL: " + e);
} catch (IOException e) {
System.out.println(
"IOException opening configURLConn: "
+ e);
}
Getting java.io.FileNotFoundException exception.
Upvotes: 2
Views: 13660
Reputation: 12757
You can try this as well.
public static void loadPropertiesToMemory() {
Properties prop = new Properties();
InputStream input = null;
String filePathPropFile = "configuration.properties";
try {
input = App.class.getClassLoader().getResourceAsStream(filePathPropFile);
prop.load(input);
} catch (IOException ex) {
ex.printStackTrace();
}
}
App
is the class where you have the above method implemented.
Upvotes: 0
Reputation: 2291
In case when your properties file in the same place with your class:
Properties properties = new Properties();
try {
properties.load(getClass().getResourceAsStream("properties.properties"));
} catch (IOException e) { /*File Not Found or something like this*/}
I case when your properties is in the root folder of your class files:
Properties properties = new Properties();
try {
properties.load(getClass().getClassLoader().getResourceAsStream("properties.properties"));
} catch (IOException e) { /*File Not Found or something like this*/}
Also you can pass the pass to your properties file with -DmyPropertiesFile=./../../properties.properties
and then get it System.getProperty("myPropertiesFile")
.
Upvotes: 3
Reputation: 61128
From the OP's comment loading is from the class path. So the ClassLoader needs to be used,
public void load() {
getClass().getResourceAsStream("my/resource");
}
public static void loadStatic() {
Thread.currentThread().getContextClassLoader().getResourceAsStream("my/resource");
}
The first method needs to be in an instance context, the second will work from a static context.
Upvotes: 0
Reputation: 45040
You can load your properties file in java class in this way:-
InputStream fileStream = new FileInputStream(configPropertiesFile);
currConfigProperties.load(fileStream);
Also, change your property file path to src/com/abc/applet/Configuration.properties
Also you can use this to load it from the classpath:-
currConfigProperties.load(this.getClass().getResourceAsStream(configPropertiesFile));
Upvotes: 1