尝试在插件类型项目中添加log4j。错误:java.lang.NoClassDefFoundError:org / apache / log4j / PropertyConfigurator

时间:2018-08-23 05:51:27

标签: java log4j

我正试图在插件项目中添加Log4j.jar以进行日志记录。我通过使用构建路径->添加外部jar添加了log4j jar。之后,我尝试运行插件项目,但出现错误

  

java.lang.NoClassDefFoundError:org / apache / log4j / PropertyConfigurator。

     

如果我使用java main方法运行相同的代码,它将正常工作。

public HHLogger(Object className) {
    DateTimeFormatter df  = DateTimeFormatter.ofPattern("ddMMYYYY");
    System.setProperty("filename", df.format(LocalDate.now()));
    PropertyConfigurator.configure(".\\resources\\log4j.properties");
    logger = Logger.getLogger(className.getClass());
}



public static void main(String args[]) {
    DateTimeFormatter df  = DateTimeFormatter.ofPattern("ddMMYYYY");
    System.setProperty("filename", df.format(LocalDate.now()));
    PropertyConfigurator.configure(".\\resources\\log4j.properties");
    logger = Logger.getLogger(HHLogger.class);
    logger.info(" 999 Welcom");

}

java.lang.NoClassDefFoundError:org / apache / log4j / PropertyConfigurator     在com.americanexpress.connecthotelforgbt.utility.HHLogger。(HHLogger.java:22)     在com.americanexpress.connecthotelforgbt.parsers.OhhRqRsParser。(OhhRqRsParser.java:104)     在com.americanexpress.connecthotelforgbt.DataIntelliSense.upshell命令(DataIntelliSense.java:322)     在com.americanexpress.connecthotelforgbt.listener.CommandModificationService.process(CommandModificationService.java:102)     在com.sabre.edge.cf.core.executors.PublicServiceExecutor.decide(PublicServiceExecutor.java:143)     在org.jbpm.graph.node.Decision.execute(Decision.java:85)     在org.jbpm.graph.def.Node.enter(Node.java:314)     在org.jbpm.graph.def.Transition.take(Transition.java:151)     在org.jbpm.graph.def.Node.leave(Node.java:389)     在org.jbpm.graph.node.StartState.leave(StartState.java:70)     在org.jbpm.graph.exe.Token.signal(Token.java:192)     在org.jbpm.graph.exe.Token.signal(Token.java:140)     在org.jbpm.graph.exe.ProcessInstance.signal(ProcessInstance.java:271)     在com.sabre.edge.cf.core.flow.JBPMFlowExecutor.execute(JBPMFlowExecutor.java:45)     在com.sabre.edge.cf.core.SRWRuntime.executeWorkflow(SRWRuntime.java:238)     在com.sabre.edge.cf.core.SRWRuntime.process(SRWRuntime.java:173)     在com.sabre.edge.cf.emu.bridge.EmulatorBridge.processServiceContext(EmulatorBridge.java:198)     在com.sabre.edge.cf.emu.bridge.EmulatorBridge.updateCommand(EmulatorBridge.java:87)     在com.sabre.stn.emulator.core.bridge.BridgeFilter.preSend(BridgeFilter.java:80)     在com.sabre.stn.emulator.basic.model.BasicEmulatorModel.sendPreProcess(BasicEmulatorModel.java:3737)     在com.sabre.stn.emulator.basic.model.BasicEmulatorModel.sendMessage(BasicEmulatorModel.java:3596)     在com.sabre.stn.emulator.basic.model.BasicEmulatorModel.doEnter(BasicEmulatorModel.java:3516)     在com.sabre.stn.emulator.basic.model.BasicEmulatorModel.enter(BasicEmulatorModel.java:1749)     在com.sabre.stn.emulator.basic.BasicEmulator.enter(BasicEmulator.java:1979)     在com.sabre.stn.emulator.core.EmuManager.emptyTheKeyBuffer(EmuManager.java:1132)     在com.sabre.stn.emulator.core.KeysToScreenThread.run(KeysToScreenThread.java:78) 引起原因:java.lang.ClassNotFoundException:org.apache.log4j.PropertyConfigurator     在org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:506)     在org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:422)     在org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:410)     在org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)     在java.lang.ClassLoader.loadClass(未知来源)     ...另外26个

1 个答案:

答案 0 :(得分:0)

该错误消息很清楚,无法找到PropertyConfigurator的类文件 确保类文件PropertyConfigurator在您的类路径中,或者如果它在jar文件中,则同样适用。它表明PropertyConfigurator是在编译时找到的,而在运行时找不到的。也许您只需要将其添加到类路径中即可。

请参阅以下内容可以帮助您:How to solve java.lang.NoClassDefFoundError?

ps:如果两个类都在同一文件中,则您不能这样做,因为它们都是公共的。