3

I have a basic cmd program that calls a jar which has an Install class with the main method. The Install class uses a java.util.logging.config.file from its resources folder.

private static void setupLogging() {
    if (System.getProperty("java.util.logging.config.file") == null) {
        final InputStream inputStream = Install.class.getResourceAsStream("/logging.properties");
        try {
            LogManager.getLogManager().readConfiguration(inputStream);
        } catch (final IOException e) {
            Logger.getAnonymousLogger().severe("Could not load default logging.properties file");
            Logger.getAnonymousLogger().severe(e.getMessage());
        }
    }
}

This install program belongs to a different project and I shouldn't change it. But I want to use a seperate logging.properties file from my disk (preferably from the same folder where I have the cmd program). How do I force this install program to use my logging.properties file instead of it's default one from the resources folder? Is there a way I can force this?

glts
  • 19,167
  • 11
  • 65
  • 85

1 Answers1

1

You could try this:

String yourFilePath = "./yourLogProperties.properties";
LogManager.getLogManager().readConfiguration(new FileInputStream(yourFilePath);

Properties will be loaded from a file stored in the same folder which your app is executed.

Update:

I was searching a little bit how to pass properties thru command line and there's a pretty good explanation here. So, in your case you'll need something like this:

java -jar -Djava.util.logging.config.file="./yourLogProperties.properties" YourApp.jar

Also you can test if it works making a simple test:

public class Tests {    
    public static void main(String[] args) {
        System.out.println(System.getProperty("java.util.logging.config.file"));
    }    
}

Running this from command line you should get the answer ./yourLogProperties.properties

Community
  • 1
  • 1
dic19
  • 17,446
  • 6
  • 35
  • 64
  • Thanks for the input. But the problem here is that I still have to modify the Install class for this. I want to be able to specify the properties file thru command line, is that possible? – Nidhish Puthiyadath Jul 11 '13 at 20:09
  • You're wellcome. In that case, you should take a look to [this](http://stackoverflow.com/questions/7351533/set-multiple-system-properties-java-command-line). You'll need something like `java -jar -Djava.util.logging.config.file="./yourLogProperties.properties"` I didn't make any test so I can't be shure if it will work. Try and let us know if so! – dic19 Jul 11 '13 at 20:27
  • Thanks. That seemed to work.For some reason I was thinking that I should force the java.util.logging File to be a system property. – Nidhish Puthiyadath Jul 11 '13 at 21:35