0

I am just begining in Log4J an I am facing an issue that I don't realy understand. I am using Eclipse IDE, and after compilation the got this message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at test.log.Program.main(Program.java:20)

See below the code I wrote

static Logger logger = Logger.getLogger(Program.class);
/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException{                      
    PropertyConfigurator.configure(args[0]);
    logger.info("Hello PropertyConfigurator");
}

Please advise.

Regards.

Harry Coder
  • 2,429
  • 2
  • 28
  • 32

1 Answers1

1

The issue isn't related to Log4j. You are probably calling your program without an argument, which means that args[0] isn't defined. Try this:

static Logger logger = Logger.getLogger(Program.class);
/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException{
  if (args.length > 0){ 
    PropertyConfigurator.configure(args[0]);
  }
  logger.info("Hello PropertyConfigurator");
}

Now it should work regardless of the presence of a parameter.

Kai Sternad
  • 22,214
  • 7
  • 47
  • 42
  • Hi, it is me again. I edit my code like in your answer, but now I have the following error:log4j:WARN No appenders could be found for logger (test.log.Program). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. – Harry Coder Mar 20 '12 at 12:53
  • @user893953 see http://stackoverflow.com/questions/1140358/how-to-initialize-log4j-properly – Kai Sternad Mar 20 '12 at 20:21