0

I am testing a library (jar) that is using property (mytest.properties). They way the library (jar) loads the property is by doing

                ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
                InputStream input = classLoader.getResourceAsStream("mytest.properties");

So what I want to test is what happens when the property file exist and when it does not exit. In order to test this I need to edit the property file once the JVM is started. I have tried doing that and does not work. Bellow is the code I tried to edit the property file but this always returns empty string.

Content of main_mytest.properties is:

a=hello world
b=hello java

Content of mytest.properties and empty.txt is empty.

""

My Class is:

    import org.apache.commons.io.IOUtils;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.StringWriter;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.StandardCopyOption;

    public class MyPropertyFiles {


        final static String resourcesPath = "./mytestproj/src/main/resources";

        public static void main(String [] args) throws IOException {

            Path source = Paths.get(resourcesPath + "/main_mytest.properties");
            Path destination = Paths.get(resourcesPath + "/mytest.properties");
            Path empty = Paths.get(resourcesPath + "/empty.txt");

            try
            {
                Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);

                ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
                InputStream input = classLoader.getResourceAsStream("mytest.properties");
                StringWriter writer = new StringWriter();
                IOUtils.copy(input, writer, "utf-8");
                String theString = writer.toString();
                System.out.println("!!!!!!!!!!!!!!!! The String: \n" + theString);

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally {
                Files.copy(empty, destination, StandardCopyOption.REPLACE_EXISTING);
            }

        }
    }
ankit s.
  • 93
  • 6
  • Are you sure that `mytest.properties` is writable with your current privileges? Try placing a debug point after the first `Files.copy` statement and looking at the contents of the file on disk. That might help. – Sean Glover Jan 13 '16 at 20:03
  • content of the file gets update. But the update content is not in `classLoader.getResourceAsStream("mytest.properties");` – ankit s. Jan 14 '16 at 02:48
  • The test properties file you are working with is probably not on your classpath, which is where the `ClassLoader` is looking for the resource using the name you gave it. Check this answer: http://stackoverflow.com/questions/19035407/classloader-getresourceasstream-returns-null – Sean Glover Jan 14 '16 at 02:59
  • The test properties file I am are working with is in the classpath but what I am doing is updating the file after the JVM has started. And trying to reload the property file. After doing some digging I don't think what I am doing is allowed in java. – ankit s. Jan 22 '16 at 23:15

1 Answers1

0

After doing some digging I don't think reloading the files in the ClassLoader after the JVM has started is allowed.

ankit s.
  • 93
  • 6