2

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.

3
  • I guess this will work => configPropertiesFile = "src/com/abc/applet/Configuration.properties" Commented Mar 19, 2013 at 11:24
  • Presumably this is an applet loading from a remote server? Or are you trying to read from the classpath? Either way, try the ClassLoader.getResourceAsStream method. Commented Mar 19, 2013 at 11:25
  • @bmorris591 - classpath Commented Mar 19, 2013 at 11:26

4 Answers 4

3

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").

Sign up to request clarification or add additional context in comments.

Comments

1

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));

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.