2012-03-19 93 views
0

我只是在Log4J中開始了一個我面臨的問題,我並沒有真正理解。 我使用的Eclipse IDE,並編譯後得到這個消息:在線程 「主要」 java.lang.ArrayIndexOutOfBoundsException在Log4J中遇到問題

例外:0 在test.log.Program.main(Program.java:20)

見我寫

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

請告知下面的代碼。

問候。

回答

1

該問題與Log4j無關。你可能在沒有參數的情況下調用你的程序,這意味着args [0]沒有被定義。試試這個:

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

現在它應該工作,不管參數的存在。

+0

嗨,這是我的一次。我編輯我的代碼就像在你的答案中,但現在我有以下錯誤:log4j:警告沒有appender可以找到記錄器(test.log.Program)。 log4j:WARN請正確初始化log4j系統。 log4j:WARN請參閱http://logging.apache.org/log4j/1.2/faq.html#noconfig瞭解更多信息。 – 2012-03-20 12:53:17

+0

@ user893953請參閱http://stackoverflow.com/questions/1140358/how-to-initialize-log4j-properly – 2012-03-20 20:21:59

+0

嗨,現在好了。我的xml文件不在正確的文件夾中! TX – 2012-05-23 11:14:37